본문 바로가기

Algorithm/프로그래머스

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

반응형

프로그래머스 : 베스트앨범

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 


- 문제

  노래 장르당 최대 두 개씩 play 수가 많은 것들의 번호를 반환하는 문제다.

 


- 풀이

#include <string>
#include <vector>
#include <map>
#include <algorithm>
// #include <iostream>

using namespace std;

bool cmp(pair<string,int> a, pair<string,int> b){
    return a.second > b.second;
}
bool comp(pair<int,pair<string,int>> a, pair<int,pair<string,int>> b){
    if(a.first == b.first){
        return a.second.second < b.second.second;
    }
    else{
        return a.first > b.first;
    }
}

vector<int> solution(vector<string> genres, vector<int> plays) {
    vector<int> answer;
    vector<pair<int,pair<string,int>>> v;
    map<string,int> checkM;
    for(int i = 0; i < genres.size(); i++){
        v.push_back({plays[i],{genres[i],i}});
        checkM[genres[i]] += plays[i];
    }
    sort(v.begin(),v.end(),comp);
    vector<pair<string,int>> checkV(checkM.begin(),checkM.end());
    sort(checkV.begin(),checkV.end(),cmp);
    // for(auto a : checkV){
    //     cout << a.first << ' ' << a.second << '\n';
    // }
    for(auto a : checkV){
        int count = 0;
        for(auto b : v){
            if(b.second.first == a.first){
                answer.push_back(b.second.second);
                count++;
            }
            if(count == 2){
                break;
            }
        }
    }
    
    return answer;
}

 

 

map은 sort가 처음에 greater 이 정도 밖에 안되므로 vector<pair>로 바꿔줘야 함.

 

  


- 기억할 것!

 X

반응형