ํด๊ฒฐ๋ฐฉ๋ฒ
- ์ด ๋ ๊ฐ์ง์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ์
- 1) ๋ง์ด ์ฌ์๋ ์ฅ๋ฅด๊ฐ ๋ญ์ง → key๋ฅผ ์ฅ๋ฅด๋ก ํ๊ณ value์ ๊ฐ์ ์ฅ๋ฅด์ ์ ์ฒด ์ฌ์ ํ์๋ฅผ ๋์
- 2) ๊ฐ์ ์ฅ๋ฅด ๋ด์์ ๋ง์ด ์ฌ์๋ ๋ ธ๋ ์์ → ๊ณ ์ ๋ฒํธ, ์ฅ๋ฅด, ์ฌ์ํ์๋ฅผ ๊ฐ์ง๋ struct๋ฅผ ๋ง๋ค์ด์ sort ํด์ค
- ๋ ๊ฐ๋ฅผ ์ฌ์ ํ์๋ฅผ ๊ธฐ์ค์ผ๋ก, ๋ด๋ฆผ์ฐจ์(DESC) ์ ๋ ฌํด์คฌ์
์ฝ๋
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
struct Music
{
int idx=0;
string genre;
int play = 0;
Music(int _idx, string _g, int _p):idx(_idx), genre(_g), play(_p){}
};
map<string, int> maxGenre;
bool desc(pair<string, int> &aa, pair<string, int> &bb)
{
return aa.second > bb.second;
}
bool compare(Music a, Music b)
{
return a.play > b.play;
}
void insert(string genre, int play)
{
auto it = maxGenre.find(genre);
if( it != maxGenre.end() )
it->second += play;
else
maxGenre[genre] = play;
}
vector<int> solution(vector<string> genres, vector<int> plays) {
vector<int> answer;
vector<Music> music;
for(int i=0; i<genres.size(); i++)
{
Music msc(i, genres[i], plays[i]);
music.push_back(msc);
insert(genres[i], plays[i]);
}
vector<pair<string, int>> vec(maxGenre.begin(), maxGenre.end());
sort(vec.begin(), vec.end(), desc);
sort(music.begin(), music.end(), compare);
int cnt = 1;
for(auto v : vec)
{
cnt = 1;
for(int i=0; i<music.size(); i++)
{
if(v.first != music[i].genre)
continue;
answer.push_back(music[i].idx);
if( cnt == 2 ) break;
cnt++;
}
}
return answer;
}
'๐Algorithm ------------ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค - level3/์์๋๋ฐ์ ์์์ต๋๋ค/MySql (0) | 2023.05.19 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค - level2/๊ฐ์ฅํฐ์/C++ (0) | 2023.05.15 |
ํ๋ก๊ทธ๋๋จธ์ค - level 3/์ต๊ณ ์ ์งํฉ/C++ (4) | 2023.05.12 |
ํ๋ก๊ทธ๋๋จธ์ค - ํ์๋ฒ(Greedy)/์ฒด์ก๋ณต/C++ (0) | 2021.08.02 |
ํ๋ก๊ทธ๋๋จธ์ค - ์์ ํ์/๋ชจ์๊ณ ์ฌ/C++ (0) | 2021.07.30 |