코딩공작소

[프로그래머스]베스트앨범 본문

알고리즘/시뮬레이션

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

안잡아모찌 2019. 10. 24. 00:37

https://programmers.co.kr/learn/courses/30/lessons/42579#

 

코딩테스트 연습 - 베스트앨범 | 프로그래머스

스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가 많이 재생된 장르를 먼저 수록합니다. 장르 내에서 많이 재생된 노래를 먼저 수록합니다. 장르 내에서 재생 횟수가 같은 노래 중에서는 고유 번호가 낮은 노래를 먼저 수록합니다. 노래의 장르를 나타내는 문자열 배열 genres와 노래별 재생 횟수를 나타내는 정수 배열 play

programmers.co.kr

 

문제를 잘읽자

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct INFO{
    int idx;
    string name;
    int total_cnt;
    int cnt;
};
 
bool cmp(const INFO a,const INFO b){
    if(a.total_cnt > b.total_cnt) return true;
    else if(a.total_cnt < b.total_cnt) return false;
    else {
        if(a.cnt > b.cnt) return true;
        else if(a.cnt < b.cnt) return false;
        else{
            if(a.idx < b.idx) return true;
            else return false;
        }
    }
}
 
vector<int> solution(vector<string> genres, vector<int> plays) {
    vector<int> answer;
    vector<INFO> v;
    unordered_map<string,int> m;
    for(int i=0;i<genres.size();i++) m[genres[i]]+=plays[i];
    for(int i=0;i<genres.size();i++) v.push_back({i,genres[i],m[genres[i]],plays[i]}); 
    sort(v.begin(),v.end(),cmp);
    string name ="";
    for(int i=0;i<v.size()-1;i++){
        if(v[i].name==name)continue;
        if(v[i].name==v[i+1].name){
            name=v[i].name;
            answer.push_back(v[i].idx);
            answer.push_back(v[i+1].idx);
            i++;
        }else{
            if(v[i].total_cnt==v[i].cnt) answer.push_back(v[i].idx);
        }
    }if(v[v.size()-1].total_cnt==v[v.size()-1].cnt) answer.push_back(v[v.size()-1].idx);
    
    return answer;
}
cs

'알고리즘 > 시뮬레이션' 카테고리의 다른 글

[프로그래머스]가장 큰 수  (0) 2019.10.25
[프로그래머스]기능개발  (0) 2019.10.24
[프로그래머스]전화번호목록  (1) 2019.10.23
[프로그래머스]hash 구현  (0) 2019.10.23
[백준]나무제테크(2)  (0) 2019.10.09