HashMap
, PriorityQueue
, compareTo
https://school.programmers.co.kr/learn/courses/30/lessons/42579
import java.util.*;
class Solution {
public int[] solution(String[] genres, int[] plays) {
Map<String, Integer> genreMap = new HashMap<>();
Map<String, List<Music>> musicMap = new HashMap<>();
List<Integer> ansList = new ArrayList<>();
for(int i=0; i<genres.length; i++) {
genreMap.put(genres[i], genreMap.getOrDefault(genres[i], 0) + plays[i]);
if(!musicMap.containsKey(genres[i])) {
musicMap.put(genres[i], new ArrayList<>());
}
musicMap.get(genres[i]).add(new Music(plays[i], i));
}
PriorityQueue<GenreInfo> genrePQ = new PriorityQueue<>();
for(String genre : genreMap.keySet()) {
genrePQ.offer(new GenreInfo(genre, genreMap.get(genre)));
}
while(!genrePQ.isEmpty()) {
// 1번 기준
GenreInfo genreInfo = genrePQ.poll();
PriorityQueue<Music> musicPQ = new PriorityQueue<>();
for(Music music : musicMap.get(genreInfo.genre)) {
musicPQ.offer(music);
}
for(int i=0; i<2; i++) {
// 장르에 속한 곡이 하나인 경우 예외처리
if(musicPQ.isEmpty()) {
break;
}
Music music = musicPQ.poll();
ansList.add(music.num);
}
}
return ansList.stream().mapToInt(Integer::intValue).toArray();
}
public static class GenreInfo implements Comparable<GenreInfo> {
String genre;
int totalPlayCnt;
public GenreInfo(String genre, int totalPlayCnt) {
this.genre = genre;
this.totalPlayCnt = totalPlayCnt;
}
@Override
public int compareTo(GenreInfo g) { // 1번 기준
return g.totalPlayCnt - this.totalPlayCnt;
}
}
public static class Music implements Comparable<Music> {
int playCnt;
int num;
public Music(int playCnt, int num) {
this.playCnt = playCnt;
this.num = num;
}
@Override
public int compareTo(Music m) {
if(this.playCnt == m.playCnt) { // 3번 기준
return this.num - m.num;
}
return m.playCnt - this.playCnt; // 2번 기준
}
}
}
import java.util.*;
class Solution {
public int[] solution(String[] genres, int[] plays) {
Map<String, Integer> genreMap = new HashMap<>();
Map<String, List<Music>> musicMap = new HashMap<>();
List<Integer> ansList = new ArrayList<>();
for(int i=0; i<genres.length; i++) {
genreMap.put(genres[i], genreMap.getOrDefault(genres[i], 0) + plays[i]);
if(!musicMap.containsKey(genres[i])) {
musicMap.put(genres[i], new ArrayList<>());
}
musicMap.get(genres[i]).add(new Music(plays[i], i));
}
List<String> genreList = new ArrayList<>(genreMap.keySet());
Collections.sort(genreList, ((o1, o2) -> genreMap.get(o2) - genreMap.get(o1)));
for (String genre : genreList) {
PriorityQueue<Music> musicPQ = new PriorityQueue<>();
for(Music music : musicMap.get(genre)) {
musicPQ.offer(music);
}
for(int i=0; i<2; i++) {
// 장르에 속한 곡이 하나인 경우 예외처리
if(musicPQ.isEmpty()) {
break;
}
Music music = musicPQ.poll();
ansList.add(music.num);
}
}
return ansList.stream().mapToInt(Integer::intValue).toArray();
}
public static class Music implements Comparable<Music> {
int playCnt;
int num;
public Music(int playCnt, int num) {
this.playCnt = playCnt;
this.num = num;
}
@Override
public int compareTo(Music m) {
if(this.playCnt == m.playCnt) { // 3번 기준
return this.num - m.num;
}
return m.playCnt - this.playCnt; // 2번 기준
}
}
}
30분
stream과 람다식 적극 활용하기