import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
class Solution {
static class Song implements Comparable<Song> {
int id, play;
String genre;
Song(int id, int play, String genre) {
this.id = id;
this.play = play;
this.genre = genre;
}
@Override
public int compareTo(Song o) {
if (this.play == o.play) {
return this.id - o.id;
} else {
return -(this.play - o.play);
}
}
}
public int[] solution(String[] genres, int[] plays) {
ArrayList<Song> songList = new ArrayList<>();
;
HashMap<String, Integer> total_genres_map = new HashMap<>();
for (int i = 0; i < genres.length; i++) {
songList.add(new Song(i, plays[i], genres[i]));
if (total_genres_map.containsKey(genres[i]))
total_genres_map.put(genres[i], total_genres_map.get(genres[i]) + plays[i]);
else
total_genres_map.put(genres[i], plays[i]);
}
Collections.sort(songList, new Comparator<Song>() {
@Override
public int compare(Song s1, Song s2) {
if (s1.genre.equals(s2.genre)) {
return s1.compareTo(s2);
} else {
return -(total_genres_map.get(s1.genre) - total_genres_map.get(s2.genre));
}
}
});
HashMap<String, Integer> albumMap = new HashMap<>();
ArrayList<Integer> bestAlbum = new ArrayList<>();
for (Song song : songList) {
if(!albumMap.containsKey(song.genre))
{
albumMap.put(song.genre, 1);
bestAlbum.add(song.id);
}
else if(albumMap.get(song.genre)==1)
{
albumMap.put(song.genre, 2);
bestAlbum.add(song.id);
}
else if(albumMap.get(song.genre)==2)
continue;
}
int[] answer = new int[bestAlbum.size()];
for(int i = 0 ; i < answer.length ; ++i)
answer[i] = bestAlbum.get(i);
return answer;
}
}
Collections.sort(songList, new Comparator() {
@Override
public int compare(Song s1, Song s2) {
if (s1.genre.equals(s2.genre)) {
return s1.compareTo(s2);
} else {
return -(total_genres_map.get(s1.genre) - total_genres_map.get(s2.genre));
}
}
});
음수 또는 0이면 객체의 자리가 그대로 유지되며, 양수인 경우에는 두 객체의 자리가 변경된다.