wow...
String형의 비교를 != 로 해서 다 풀어놓고 계속 틀렸었다.
https://youngjinmo.github.io/2019/12/boolean-string/
내일 정리해야지
import java.util.*;
public class Solution_BestAlbum {
public static int[] solution(String[] genres, int[] plays) {
ArrayList<Music> list = new ArrayList<>();
ArrayList<Integer> bestList = new ArrayList<>();
Map<String, Integer> genreMap = new HashMap<>();
Map<String, Integer> musicMap = new HashMap<>();
for(int i = 0; i < genres.length; i ++) {
list.add(new Music(i, genres[i], plays[i]));
if(genreMap.containsKey(genres[i])) {
genreMap.put(genres[i], genreMap.get(genres[i]) + plays[i]);
} else {
genreMap.put(genres[i], plays[i]);
}
}
Collections.sort(list, new Comparator<Music>() {
@Override
public int compare(Music o1, Music o2) {
if(o1.genre.equals(o2.genre)) {
System.out.println("장르가 같을때 주소");
System.out.println(System.identityHashCode(o1.genre));
System.out.println(System.identityHashCode(o2.genre));
if(o1.play == o2.play) {
return o1.id - o2.id;
} else {
return o2.play - o1.play;
}
} else {
return genreMap.get(o2.genre) - genreMap.get(o1.genre);
}
}
});
for(Music music : list) {
if(!musicMap.containsKey(music.genre)) {
musicMap.put(music.genre, 1);
bestList.add(music.id);
} else {
int genreCnt = musicMap.get(music.genre);
if(genreCnt >= 2)
continue;
else {
musicMap.put(music.genre, genreCnt + 1);
bestList.add(music.id);
}
}
}
int[] answer = new int[bestList.size()];
for(int i = 0; i < answer.length; i ++) {
answer[i] = bestList.get(i);
}
return answer;
}
/*
public static void main(String[] args) {
String[] genres = {"classic", "pop", "classic", "classic", "pop"};
int[] plays = {500, 600, 150, 800, 2500};
int[] answer = solution(genres, plays);
for(int i = 0; i < answer.length; i ++) {
System.out.println(answer[i]);
}
}
*/
}
class Music {
int id;
String genre;
int play;
Music(int i, String g, int p) {
this.id = i;
this.genre = g;
this.play = p;
}
}