Skip to content

Commit 3c63f23

Browse files
committed
[level 3] Title: 베스트앨범, Time: 4.00 ms, Memory: 87.2 MB -BaekjoonHub
1 parent 731f8a5 commit 3c63f23

File tree

2 files changed

+48
-53
lines changed

2 files changed

+48
-53
lines changed

프로그래머스/3/42579. 베스트앨범/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 74.5 MB, 시간: 19.06 ms
7+
메모리: 87.2 MB, 시간: 4.00 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 09월 01일 17:02:31
19+
2025년 09월 06일 12:54:03
2020

2121
### 문제 설명
2222

프로그래머스/3/42579. 베스트앨범/베스트앨범.java

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,71 @@
11
import java.util.*;
22
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+
327
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<>(); // 장르: [고유번호, 플레이]
930

10-
for(int i = 0; i< length; i++){
31+
for(int i = 0; i < genres.length; i++) {
1132
String genre = genres[i];
12-
int cnt = plays[i];
33+
int play = plays[i];
1334

14-
genreMap.put(genre, genreMap.getOrDefault(genre,0) + cnt);
35+
genreMap.put(genre, genreMap.getOrDefault(genre,0) + play);
1536

16-
int[] info = {i,cnt};
17-
// 기존에 이미 등록된 장르
37+
Songs song = new Songs(i, play);
1838
if(songsMap.containsKey(genre)){
19-
songsMap.get(genre).add(info);
39+
songsMap.get(genre).add(song);
2040
} 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);
2444
}
2545
}
2646

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>(){
3050
@Override
3151
public int compare(String s1, String s2){
3252
return genreMap.get(s2) - genreMap.get(s1);
3353
}
3454
});
3555

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); // 고유 번호 등록
5664
cnt ++;
5765
}
5866
}
5967

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()
7469
.mapToInt(Integer::intValue)
7570
.toArray();
7671
}

0 commit comments

Comments
 (0)