2 분 소요

문제

스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다.

  1. 속한 노래가 많이 재생된 장르를 먼저 수록합니다.
  2. 장르 내에서 많이 재생된 노래를 먼저 수록합니다.
  3. 장르 내에서 재생 횟수가 같은 노래 중에서는 고유 번호가 낮은 노래를 먼저 수록합니다.

노래의 장르를 나타내는 문자열 배열 genres와 노래별 재생 횟수를 나타내는 정수 배열 plays가 주어질 때, 베스트 앨범에 들어갈 노래의 고유 번호를 순서대로 return 하도록 solution 함수를 완성하세요.

제한사항
  • genres[i]는 고유번호가 i인 노래의 장르입니다.
  • plays[i]는 고유번호가 i인 노래가 재생된 횟수입니다.
  • genres와 plays의 길이는 같으며, 이는 1 이상 10,000 이하입니다.
  • 장르 종류는 100개 미만입니다.
  • 장르에 속한 곡이 하나라면, 하나의 곡만 선택합니다.
  • 모든 장르는 재생된 횟수가 다릅니다.
입출력 예
genres plays return
[“classic”, “pop”, “classic”, “classic”, “pop”] [500, 600, 150, 800, 2500] [4, 1, 3, 0]
입출력 예 설명

classic 장르는 1,450회 재생되었으며, classic 노래는 다음과 같습니다.

  • 고유 번호 3: 800회 재생
  • 고유 번호 0: 500회 재생
  • 고유 번호 2: 150회 재생

pop 장르는 3,100회 재생되었으며, pop 노래는 다음과 같습니다.

  • 고유 번호 4: 2,500회 재생
  • 고유 번호 1: 600회 재생

따라서 pop 장르의 [4, 1]번 노래를 먼저, classic 장르의 [3, 0]번 노래를 그다음에 수록합니다.

※ 공지 - 2019년 2월 28일 테스트케이스가 추가되었습니다.

C++

#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>

using namespace std;

struct Priority
{
    bool operator<(const Priority p) const
    {
        if (this->play_count < p.play_count)
            return true;

        if (this->play_count == p.play_count)
        {
            if (p.index < this->index)
                return true;
        }

        return false;
    }

    int index;
    int play_count;
};

vector<int> solution(vector<string> genres, vector<int> plays)
{
    vector<int> answer;
    unordered_map<string, int> best_map;
    unordered_map<string, vector<int>> count_map;
    priority_queue<Priority> pq;

    for (size_t i = 0; i < genres.size(); i++)
    {
        best_map[genres[i]] += plays[i];
        count_map[genres[i]] = {};
        Priority p;
        p.index = i;
        p.play_count = plays[i];
        pq.push(p);
    }

    vector<pair<string, int>> sort_array(best_map.begin(), best_map.end());
    sort(sort_array.begin(), sort_array.end(), [](pair<string, int> lhs, pair<string, int> rhs)
         { return lhs.second > rhs.second; });

    while (!pq.empty())
    {
        Priority p = pq.top();
        pq.pop();

        string genres_name = genres[p.index];
        vector<int> count = count_map[genres_name];
        if (count.size() == 2)
            continue;

        count.push_back(p.index);
        count_map[genres_name] = count;
    }

    for (auto best : sort_array)
    {
        vector<int> count = count_map[best.first];
        for (int cnt : count)
            answer.push_back(cnt);
    }

    return answer;
}

테스트케이스

int main(void)
{
    vector<int> answer;
    answer = solution({"A", "B", "A", "A", "B"}, {500, 600, 150, 800, 2500}); // 4, 1, 3, 0
    answer = solution({"B", "A"}, {500, 600}); // 1, 0
    answer = solution({"B"}, {500}); // 0
    answer = solution({"B", "A"}, {600, 500}); // 0, 1
    answer = solution({"A", "B"}, {600, 500}); // 0, 1
    answer = solution({"A", "A", "B"}, {600, 500, 300}); // 0, 1, 2
    answer = solution({"A", "B", "A"}, {600, 500, 600}); // 0, 2, 1
    answer = solution({"A", "B", "A"}, {600, 500, 700}); // 2, 0, 1
    answer = solution({"A", "A", "A"}, {600, 500, 700}); // 2, 0
    answer = solution({"A", "A", "A"}, {3, 2, 1}); // 0, 1
    answer = solution({"A", "A", "B", "A", "B", "B"}, {5, 5, 6, 5, 7, 7});  // 4, 5, 0, 1
    cout << "===============" << endl;
    for (auto a : answer)
        cout << a << " ";
    cout << endl;
}

마치며

길다.. 내 소스 코드 길다.. 아무리 생각해봐도 다른사람 풀이를 보기 전에는 이렇게 할 수 밖에 없어서 길게 작성했다. 피지컬이 필요한 문제였다. 한동안 C++를 안하고 있어서 많이 헤맨 문제다.

댓글남기기