프로그래밍 공방

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

개발/문제해결

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

hyosupsong 2021. 1. 20. 21:26

문제

programmers.co.kr/learn/courses/30/lessons/42579

 

코딩테스트 연습 - 베스트앨범

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

programmers.co.kr

문제해결방법

장르를 key, <고유번호, 재생횟수>를 저장하는 우선순위 큐를 value로 하는 HashMap과

장르를 key, 장르의 재생횟수를 value로 하는 HashMap 두 개를 사용해서 문제를 풀었다.

코드

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
70
71
72
73
74
75
package Programmers;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
 
class Music implements Comparable<Music> {
    String genre;
    int count;
    Music(String genre, int count) {
        this.genre = genre;
        this.count = count;
    }
 
    @Override
    public int compareTo(Music arg0) {
        return Integer.compare(arg0.count, this.count);
    }
}
 
public class Solution_베스트앨범 {
    
 
    public static int[] solution(String[] genres, int[] plays) {
        Map<String, PriorityQueue<int[]>> musicByGenres = new HashMap<>();
        Map<String, Integer> numberOfPlays = new HashMap<>();
        for(int i=0; i<genres.length; i++) {
            if(numberOfPlays.containsKey(genres[i])) {
                numberOfPlays.put(genres[i], numberOfPlays.get(genres[i])+plays[i]);
            } else numberOfPlays.put(genres[i], plays[i]);
 
            if(musicByGenres.containsKey(genres[i])) {
                musicByGenres.get(genres[i]).add(new int[]{i, plays[i]});
            } else {
                musicByGenres.put(genres[i], new PriorityQueue<int[]>(new Comparator<int[]>() {
                    @Override
                    public int compare(int[] arg0, int[] arg1) {
                        if(arg0[1]==arg1[1]) return Integer.compare(arg0[0], arg1[0]);
                        return Integer.compare(arg1[1], arg0[1]);
                    }
                }));
                musicByGenres.get(genres[i]).add(new int[]{i, plays[i]});
            }
        }
 
        PriorityQueue<Music> pq = new PriorityQueue<>();
        for(String key : numberOfPlays.keySet()) {
            pq.add(new Music(key, numberOfPlays.get(key)));
        }
        List<Integer> list = new ArrayList<>();
        while(!pq.isEmpty()) {
            Music cur = pq.poll();
            String genre = cur.genre;
            PriorityQueue<int[]> q = musicByGenres.get(genre);
            int i=0;
            while(!q.isEmpty() && i<2) {
                list.add(q.poll()[0]);
                i++;
            }
        }
        int[] answer = new int[list.size()];
        for(int i=0; i<answer.length; i++) answer[i] = list.get(i);
        return answer;
    }
    
    public static void main(String[] args) {
        String[] genres = {"classic""pop""classic""classic""pop"};
        int[] plays = {5006001508002500};
        System.out.println(Arrays.toString(solution(genres, plays)));
    }
}
cs


코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.