mapping을 이용해 <string, int> : 동일한 string에서의 합계를 구한 후
reverse 하자.
reverse 상태에서 내림차순으로 만든 다음에
가장 큰 친구의 string을 뽑아서 전체 컨테이너 중에서 가장 plays 높은 두명의 친구를 뽑아서 삽입한다.
1) 미리 mapping해서 합계를 구했고, mapping은 하나씩 뽑아와서 비교를 하는 방식이 타당하다.는 것이다.
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
//속한 노래가 많이 재생된 장르
//장르 내에서 많이 재생된 노래
//장르 내에서 재생횟수가 같은 노래중에서 고유번호가 낮은 노래
//그 중에서 두개만 나열해 출력한다.
bool compare(pair<int,int>a, pair<int,int>b)
{
if(a.second > b.second)
return true;
return false;
}
vector<int> solution(vector<string> genres, vector<int> plays) {
vector<int> answer;
//총회수 ; classic : 1450
//총 회수 ; pop : 3100
map<string,int>total;
for(int i = 0; i < plays.size(); i++)
{
total[genres[i]] += plays[i];
}
//가장 큰 수가 위로 가도록 정렬하자
map<int,string, greater<int>>rTotal;
for(auto i : total)
{
rTotal[i.second] = i.first;
}
//일단위는 pop -> classic 순으로 만들었다.
//벡터 중에서 pop에 속한 것들만 뽑아서 정렬한다음에 두개만 뽑아내자.
for(auto iter : rTotal)
{
//인덱스 값이랑 plays
vector<pair<int,int>>tmp;
for(int i = 0; i < plays.size(); i++)
{
if(iter.second == genres[i])
{
tmp.push_back({i,plays[i] });
}
}
sort(tmp.begin(), tmp.end(), compare);
int cnt = 0;
//장르가 한개일 수 있다.
for(int i = 0; i < tmp.size(); i++)
{
if(cnt == 2)
break;
answer.push_back(tmp[i].first);
cnt++;
}
}
return answer;
}