public int[] solution(String[] genres, int[] plays) {
List<String> sortedGenres = sortByTotalPlays(genres, plays);
List<Integer> resultIndices = new ArrayList<>();
for (String genre : sortedGenres) {
int firstIdx = findMostPlayedSongIndex(genres, plays, genre);
resultIndices.add(firstIdx);
int secondIdx = findMostPlayedSongIndexExceptGivenIndex(genres, plays, genre, firstIdx);
if (secondIdx != -1)
resultIndices.add(secondIdx);
}
int[] result = new int[resultIndices.size()];
for (int i = 0; i < resultIndices.size(); i++) {
result[i] = resultIndices.get(i);
}
return result;
}
private List<String> sortByTotalPlays(String[] genres, int[] plays) {
Map<String, Integer> genreTotalPlaysMap = new HashMap<>();
for (int i = 0; i < genres.length; i++) {
genreTotalPlaysMap.put(genres[i], genreTotalPlaysMap.getOrDefault(genres[i], 0) + plays[i]);
}
List<String> sortedGenres = new ArrayList<>(genreTotalPlaysMap.keySet());
sortedGenres.sort((genre1, genre2) -> genreTotalPlaysMap.get(genre2) - genreTotalPlaysMap.get(genre1));
return sortedGenres;
}
private int findMostPlayedSongIndex(String[] genres, int[] plays, String genre) {
int maxPlays = 0;
int mostPlayedIndex = -1;
for (int i = 0; i < genres.length; i++) {
if (genres[i].equals(genre) && plays[i] > maxPlays) {
maxPlays = plays[i];
mostPlayedIndex = i;
}
}
return mostPlayedIndex;
}
private int findMostPlayedSongIndexExceptGivenIndex(String[] genres, int[] plays, String genre, int exceptIndex) {
int maxPlays = 0;
int secondMostPlayedIndex = -1;
for (int i = 0; i < genres.length; i++) {
if (genres[i].equals(genre) && plays[i] > maxPlays && i != exceptIndex) {
maxPlays = plays[i];
secondMostPlayedIndex = i;
}
}
return secondMostPlayedIndex;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/42579