https://school.programmers.co.kr/learn/courses/30/lessons/138477

import java.util.*;
class Solution {
public int[] solution(int k, int[] score) {
int [] answer = new int[score.length];
var lTemp = new ArrayList<Integer>();
for(int i=0; i<score.length; i++)
{
lTemp.add(score[i]);
if(i>=k)
{
lTemp.remove(Collections.min(lTemp));
}
answer[i] = Collections.min(lTemp);
}
return answer;
}
}
import java.util.*;
class Solution {
public int[] solution(int k, int[] score) {
int [] answer = new int[score.length];
var pq = new PriorityQueue<Integer>();
for(int i=0; i<score.length; i++)
{
pq.add(score[i]);
if(pq.size()>k)
{
pq.poll();
}
answer[i] = pq.peek();
}
return answer;
}
}
익힐것
1. 우선순위큐
var pq = new PriorityQueue(); : 오름차순
var pq = new PriorityQueue(Comparator.reverseOrder()); : 내림차순
pq.poll(); : pop