|
1 | 1 | import java.util.*; |
2 | 2 | class Solution { |
| 3 | + class Songs implements Comparable<Songs> { |
| 4 | + int idx; |
| 5 | + int play; |
| 6 | + |
| 7 | + public Songs(int idx, int play){ |
| 8 | + this.idx = idx; |
| 9 | + this.play = play; |
| 10 | + } |
| 11 | + |
| 12 | + @Override |
| 13 | + public String toString(){ |
| 14 | + return "idx: " + idx + ", play: " + play + "\n"; |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public int compareTo(Songs other){ |
| 19 | + if(other.play == this.play){ |
| 20 | + return this.idx - other.idx; |
| 21 | + } |
| 22 | + return other.play - this.play; |
| 23 | + } |
| 24 | + |
| 25 | + } |
| 26 | + |
3 | 27 | public int[] solution(String[] genres, int[] plays) { |
4 | | - int length = genres.length; |
5 | | - // 장르의 재생 횟수 |
6 | | - HashMap<String,Integer> genreMap = new HashMap<>(); |
7 | | - // 노래별 수록번호, 재생 횟수 |
8 | | - HashMap<String,List<int[]>> songsMap = new HashMap<>(); |
| 28 | + HashMap<String,Integer> genreMap = new HashMap<>(); // 장르,플레이 |
| 29 | + HashMap<String, List<Songs>> songsMap = new HashMap<>(); // 장르: [고유번호, 플레이] |
9 | 30 |
|
10 | | - for(int i = 0; i< length; i++){ |
| 31 | + for(int i = 0; i < genres.length; i++) { |
11 | 32 | String genre = genres[i]; |
12 | | - int cnt = plays[i]; |
| 33 | + int play = plays[i]; |
13 | 34 |
|
14 | | - genreMap.put(genre, genreMap.getOrDefault(genre,0) + cnt); |
| 35 | + genreMap.put(genre, genreMap.getOrDefault(genre,0) + play); |
15 | 36 |
|
16 | | - int[] info = {i,cnt}; |
17 | | - // 기존에 이미 등록된 장르 |
| 37 | + Songs song = new Songs(i, play); |
18 | 38 | if(songsMap.containsKey(genre)){ |
19 | | - songsMap.get(genre).add(info); |
| 39 | + songsMap.get(genre).add(song); |
20 | 40 | } else{ |
21 | | - List<int[]> newSongList = new ArrayList<>(); |
22 | | - newSongList.add(info); |
23 | | - songsMap.put(genre,newSongList); |
| 41 | + List<Songs> list = new ArrayList<>(); |
| 42 | + list.add(song); |
| 43 | + songsMap.put(genre, list); |
24 | 44 | } |
25 | 45 | } |
26 | 46 |
|
27 | | - // 정렬(장르 재생 횟수 기준 내림차순) |
28 | | - List<String> sortedGenres = new ArrayList<>(genreMap.keySet()); |
29 | | - Collections.sort(sortedGenres, new Comparator<String>(){ |
| 47 | + List<String> sortedGenre = new ArrayList<>(genreMap.keySet()); |
| 48 | + |
| 49 | + Collections.sort(sortedGenre, new Comparator<String>(){ |
30 | 50 | @Override |
31 | 51 | public int compare(String s1, String s2){ |
32 | 52 | return genreMap.get(s2) - genreMap.get(s1); |
33 | 53 | } |
34 | 54 | }); |
35 | 55 |
|
36 | | - List<Integer> best = new ArrayList<>(); |
37 | | - for(String genre: sortedGenres){ |
38 | | - List<int[]> values = songsMap.get(genre); |
39 | | - |
40 | | - Collections.sort(values, new Comparator<int[]>(){ |
41 | | - @Override |
42 | | - public int compare(int[] s1, int[] s2){ |
43 | | - if(s1[1] == s2[1]){ // 재생 횟수가 같다면 고유 번호 기준 오름차순 |
44 | | - return s1[1] - s2[1]; |
45 | | - } |
46 | | - |
47 | | - return s2[1] - s1[1]; |
48 | | - } |
49 | | - }); |
50 | | - |
51 | | - int cnt = 0; |
52 | | - for(int[] value : values){ |
53 | | - if(cnt > 1) break; |
54 | | - int number = value[0]; // 고유 번호 |
55 | | - best.add(number); |
| 56 | + ArrayList<Integer> answer = new ArrayList<>(); |
| 57 | + for(String genre : sortedGenre){ |
| 58 | + List<Songs> songs = songsMap.get(genre); |
| 59 | + Collections.sort(songs); |
| 60 | + int cnt = 1; // 한 장르당 2개씩만 |
| 61 | + for(Songs song : songs){ |
| 62 | + if(cnt > 2) break; |
| 63 | + answer.add(song.idx); // 고유 번호 등록 |
56 | 64 | cnt ++; |
57 | 65 | } |
58 | 66 | } |
59 | 67 |
|
60 | | -// System.out.print("베스트 앨범: " + best); |
61 | | - |
62 | | - |
63 | | -// for(String key: songsMap.keySet()){ |
64 | | -// System.out.print("key: " + key+", value:"); |
65 | | -// List<int[]> values = songsMap.get(key); |
66 | | -// for(int[] value: values){ |
67 | | -// System.out.print(" " + Arrays.toString(value)); |
68 | | -// } |
69 | | -// System.out.println(""); |
70 | | -// } |
71 | | - |
72 | | - |
73 | | - return best.stream() |
| 68 | + return answer.stream() |
74 | 69 | .mapToInt(Integer::intValue) |
75 | 70 | .toArray(); |
76 | 71 | } |
|
0 commit comments