Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

베스트앨범 #401

Closed
Tracked by #396
fkdl0048 opened this issue Nov 26, 2024 · 0 comments
Closed
Tracked by #396

베스트앨범 #401

fkdl0048 opened this issue Nov 26, 2024 · 0 comments
Assignees
Milestone

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Nov 26, 2024

#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;
}
  • 문제를 읽고 조건에 맞게 설정하는 것이 중요, 명확하게 풀이하려면 따로 구조체를 만들어서 사용자 정의로 정렬하는 것이 좋아 보임
  • min의 경우 둘 중에 하나의 자료형에 맞게 캐스팅하는 것이 좋을 듯
@fkdl0048 fkdl0048 self-assigned this Nov 26, 2024
@fkdl0048 fkdl0048 added this to Todo Nov 26, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Nov 26, 2024
@fkdl0048 fkdl0048 moved this from Todo to In Progress in Todo Nov 26, 2024
@fkdl0048 fkdl0048 added this to the Programmers milestone Nov 26, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Todo Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

1 participant