[프로그래머스 / C++] 베스트앨범

Seulguo·2022년 7월 14일
0

Algorithm

목록 보기
78/185
post-thumbnail

🐣 문제

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42579


🐥 코드

#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b){
    //if(a.first > b.first) return true;
    return a.first > b.first;
}

bool cmp_play(pair<string, int> a, pair<string, int> b){
    return a.second > b.second;
}

vector<int> solution(vector<string> genres, vector<int> plays) {
    vector<int> answer;
    
    unordered_map<string, vector<pair<int, int>>> hash;
    unordered_map<string, int> hash_play;
    vector<pair<string, int>> play;
    
    for(int i = 0; i < genres.size(); i++){
        hash[genres[i]].push_back(make_pair(plays[i], i));
        hash_play[genres[i]] += plays[i];
    }
    
    for(auto &song : hash){
        sort(song.second.begin(), song.second.end(), cmp);
    }
    
    play.assign(hash_play.begin(), hash_play.end());
    sort(play.begin(), play.end(), cmp_play);
    
    for(int i = 0; i < play.size(); i++){
        string name = play[i].first;
        
        for(int j = 0; (j < 2 && j < hash[name].size()); j++){
            answer.push_back(hash[name][j].second);
        }
    }
    
    cout << hash.size();
    
    
    return answer;
}

0개의 댓글