Skip to content

Commit a1e42a3

Browse files
committed
[level 3] Title: 베스트앨범, Time: 11.86 ms, Memory: 82.4 MB -BaekjoonHub
1 parent b2c2cf0 commit a1e42a3

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
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-
메모리: 87.2 MB, 시간: 4.00 ms
7+
메모리: 82.4 MB, 시간: 11.86 ms
88

99
### 구분
1010

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

1717
### 제출 일자
1818

19-
2025년 09월 06일 12:54:03
19+
2025년 09월 15일 19:38:41
2020

2121
### 문제 설명
2222

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,70 @@
11
import java.util.*;
2+
23
class Solution {
3-
class Songs implements Comparable<Songs> {
4-
int idx;
5-
int play;
4+
public class Song implements Comparable<Song> {
5+
int play; // 재생횟수
6+
int idx; // 고유 번호
67

7-
public Songs(int idx, int play){
8-
this.idx = idx;
8+
public Song(int play, int idx){
99
this.play = play;
10+
this.idx = idx;
1011
}
1112

1213
@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;
14+
public int compareTo(Song other){
15+
if(other.play == this.play) return this.idx - other.idx;
16+
else return other.play - this.play;
2317
}
24-
2518
}
2619

2720
public int[] solution(String[] genres, int[] plays) {
28-
HashMap<String,Integer> genreMap = new HashMap<>(); // 장르,플레이
29-
HashMap<String, List<Songs>> songsMap = new HashMap<>(); // 장르: [고유번호, 플레이]
21+
// 장르별 재생 횟수
22+
Map<String,Integer> genreMap = new HashMap<>();
23+
Map<String,ArrayList<Song>> songMap = new HashMap<>(); // 장르: [재생횟수, 고유번호]
3024

31-
for(int i = 0; i < genres.length; i++) {
25+
for(int i = 0; i < genres.length; i ++) {
3226
String genre = genres[i];
3327
int play = plays[i];
3428

3529
genreMap.put(genre, genreMap.getOrDefault(genre,0) + play);
3630

37-
Songs song = new Songs(i, play);
38-
if(songsMap.containsKey(genre)){
39-
songsMap.get(genre).add(song);
31+
Song song = new Song(play,i);
32+
if(songMap.containsKey(genre)){
33+
songMap.get(genre).add(song);
4034
} else{
41-
List<Songs> list = new ArrayList<>();
42-
list.add(song);
43-
songsMap.put(genre, list);
35+
ArrayList<Song> songs = new ArrayList<>();
36+
songs.add(song);
37+
songMap.put(genre,songs);
4438
}
4539
}
4640

47-
List<String> sortedGenre = new ArrayList<>(genreMap.keySet());
41+
ArrayList<String> sortedList = new ArrayList<>();
42+
for(String key : genreMap.keySet()){
43+
sortedList.add(key);
44+
}
4845

49-
Collections.sort(sortedGenre, new Comparator<String>(){
46+
// 내림차순 정렬
47+
Collections.sort(sortedList, new Comparator<String>() {
5048
@Override
5149
public int compare(String s1, String s2){
5250
return genreMap.get(s2) - genreMap.get(s1);
5351
}
5452
});
5553

5654
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); // 고유 번호 등록
64-
cnt ++;
55+
56+
for(String genre : sortedList){
57+
ArrayList<Song> songList = songMap.get(genre);
58+
Collections.sort(songList);
59+
int k = 1;
60+
61+
for(Song song : songList){
62+
if(k > 2) break;
63+
answer.add(song.idx);
64+
k++;
6565
}
6666
}
6767

68-
return answer.stream()
69-
.mapToInt(Integer::intValue)
70-
.toArray();
68+
return answer.stream().mapToInt(Integer::intValue).toArray();
7169
}
7270
}

0 commit comments

Comments
 (0)