이 프로그램에서는 매일 "명예의 전당"의 최하위 점수를 발표합니다. 예를 들어, k = 3이고, 7일 동안 진행된 가수의 점수가 [10, 100, 20, 150, 1, 100, 200]이라면, 명예의 전당에서 발표된 점수는 아래의 그림과 같이 [10, 10, 10, 20, 20, 100, 100]입니다.
명예의 전당 목록의 점수의 개수 k, 1일부터 마지막 날까지 출연한 가수들의 점수인 score가 주어졌을 때, 매일 발표된 명예의 전당의 최하위 점수를 return하는 solution 함수를 완성해주세요.
-> 자세한 내용 보러가기
import java.util.*;
class Solution {
public int[] solution(int k, int[] score) {
int[] answer = new int[score.length];
List<Integer> list = new ArrayList<>();
for(int idx = 0; idx < score.length; idx++){
if(idx < k){
list.add(score[idx]);
}else{
if(score[idx] >= list.get(0)){
list.add(score[idx]);
list.remove(0);
}
}
Collections.sort(list);
answer[idx] = list.get(0);
}
return answer;
}
}
import java.util.*;
class Solution {
public int[] solution(int k, int[] score) {
int[] answer = new int[score.length];
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int idx = 0; idx < score.length; idx++){
pq.offer(score[idx]);
if(idx >= k){
pq.poll();
}
answer[idx] = pq.peek();
}
return answer;
}
}