Skip to content

Commit

Permalink
이슈 #401에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Nov 28, 2024
1 parent 825a2bd commit b15c478
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Programmers/베스트앨범.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <iostream>

using namespace std;

struct Song {
int id;
int plays;
};

struct Genre {
int totalPlays;
vector<Song> songs;
};

vector<int> solution(vector<string> genres, vector<int> plays) {
unordered_map<string, Genre> genreMap;

for (int i = 0; i < genres.size(); i++) {
genreMap[genres[i]].totalPlays += plays[i];
genreMap[genres[i]].songs.push_back({i, plays[i]});
}

vector<pair<string, Genre>> genreList(genreMap.begin(), genreMap.end());
sort(genreList.begin(), genreList.end(), [](const auto& a, const auto& b) {
return a.second.totalPlays > b.second.totalPlays;
});

vector<int> result;

for (auto& genre : genreList) {
vector<Song>& songs = genre.second.songs;
sort(songs.begin(), songs.end(), [](const Song& a, const Song& b) {
if (a.plays == b.plays) {
return a.id < b.id;
}
return a.plays > b.plays;
});

for (size_t i = 0; i < min(songs.size(), static_cast<size_t>(2)); i++) {
result.push_back(songs[i].id);
}
}

return result;
}

0 comments on commit b15c478

Please sign in to comment.