https://programmers.co.kr/learn/courses/30/lessons/42579#
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool comp(pair<string, int> n1, pair<string, int> n2) {
return n1.second > n2.second;
}
bool compsong(pair<int, int> n1, pair<int, int> n2) {
return n1.first > n2.first;
}
vector<int> solution(vector<string> genres, vector<int> plays) {
vector<int> answer;
map<string, int> sum;
map<string, vector<pair<int, int>>> songs;
for (int i = 0; i < genres.size(); i++) {
sum[genres[i]] += plays[i];
songs[genres[i]].push_back({plays[i],i});
}
vector<pair<string, int>> vSum(sum.begin(),sum.end());
sort(vSum.begin(), vSum.end(), comp);
for (auto p : vSum) {
vector<pair<int, int>> list = songs[p.first];
sort(list.begin(), list.end(), compsong);
answer.push_back(list[0].second);
if (list.size() > 1) {
answer.push_back(list[1].second);
}
}
return answer;
}
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 백준 "2292) 벌집" (0) | 2022.06.02 |
---|---|
(c++) 백준 "10250) ACM 호텔" (0) | 2022.06.02 |
(c++) 백준 "1106) 파일 합치기" (0) | 2022.05.31 |
(c++) 1209) Sum (0) | 2022.05.28 |
(c++) SWEA "1215) 회문1" (0) | 2022.05.28 |