https://programmers.co.kr/learn/courses/30/lessons/42579
import java.util.*;
class Solution {
HashMap<String, Integer> genreSum;
public int[] solution(String[] genres, int[] plays) {
genreSum = new HashMap<>();
List<Play> playList = new ArrayList<>();
for(int i = 0; i<genres.length; i++) {
genreSum.put(genres[i], genreSum.getOrDefault(genres[i], 0)+(plays[i])) ;
playList.add(new Play(i, genres[i], plays[i]));
}
Collections.sort(playList);
List<Integer> answer = new ArrayList<>();
int genreCnt = 0;
String genre = null;
for(int i = 0; i<genres.length; i++) {
Play play = playList.get(i);
if(null == genre || !genre.equals(play.genre)) {
genre = play.genre;
genreCnt = 0;
}
if(genre.equals(play.genre) && genreCnt < 2) {
answer.add(play.index);
genreCnt++;
genre = play.genre;
}
}
return answer.stream().mapToInt(i -> i).toArray();
}
class Play implements Comparable<Play> {
Integer index;
Integer count;
String genre;
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public Play(Integer index, String genre, Integer count) {
super();
this.index = index;
this.count = count;
this.genre = genre;
}
@Override
public int compareTo(Play o) {
int indexCompare = this.index - o.index;
int countCompare = this.count - o.count;
int genreCompare = genreSum.get(this.genre) - genreSum.get(o.genre);
int compare = 0;
if(genreCompare != 0) compare = genreCompare*-1;
else if(countCompare != 0) compare = countCompare*-1;
else compare = indexCompare;
return compare;
}
}
}
'알고리즘 > 코딩테스트' 카테고리의 다른 글
프로그래머스 이중우선순위큐 (0) | 2021.09.28 |
---|---|
프로그래머스 디스크 컨트롤러 (0) | 2021.09.26 |
BACKJOON/백준 1927 최소힙 (0) | 2021.09.23 |
프로그래머스 SQL 고득점 Kit 문제 모음 2 (0) | 2021.09.19 |
프로그래머스 SQL 고득점 Kit 문제 모음 1 (0) | 2021.09.18 |