[Programmers] 베스트 앨범

Time Lapse :1hour 25min 0sec

solution.cpp

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
class INFO {
public:
string genre;
int play;
int code;
INFO(string g, int p, int c) : genre(g), play(p), code(c) {}
};
struct cmp_second {
bool operator()(pair<string, int> p1, pair<string, int> p2) {
return p1.second > p2.second;
}
};
struct cmp_myclass {
bool operator()(INFO info1, INFO info2) {
if (info1.genre == info2.genre) {
if (info1.play == info2.play) {
return info1.code < info2.code;
}
else
return info1.play > info2.play;
}
else
return info1.genre < info2.genre;
}
};
vector<int> solution(vector<string> genres, vector<int> plays) {
map<string, int> Ranking;
vector<pair<string, int>>vectorRanking;
vector<INFO> songlist;
map<string,int>::iterator it;
vector<int> answer;
for (int i = 0; i < genres.size(); i++) {
Ranking[genres[i]] += plays[i];
songlist.push_back(INFO(genres[i], plays[i], i));
}
for (auto it = Ranking.begin(); it != Ranking.end(); it++) {
vectorRanking.emplace_back(make_pair(it->first, it->second));
}
sort(vectorRanking.begin(), vectorRanking.end(), cmp_second());
sort(songlist.begin(), songlist.end(),cmp_myclass());
map<string, int> index_info;
string cur_string = songlist[0].genre;
index_info[cur_string] = 0;
for (int i = 1; i < songlist.size(); i++) {
if (songlist[i].genre == cur_string)
continue;
cur_string = songlist[i].genre;
index_info[cur_string] = i;
}
for (int i = 0; i < vectorRanking.size(); i++) {
int cnt = 0;
for (int j = index_info[vectorRanking[i].first]; j < songlist.size(); j++) {
if (songlist[j].genre == vectorRanking[i].first&&cnt != 2) {
answer.push_back(songlist[j].code);
++cnt;
}
else
break;
}
}
return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/bestAlbum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.