public int[] solution(int k, int[] score) {
int[] answer = new int[score.length];
ArrayList<Integer> temp = new ArrayList<>();
for (int i = 0; i < score.length; i++) {
if (temp.size() < k) {
insertAndSort(temp, score[i]);
} else if (temp.get(0) < score[i]) {
temp.remove(0);
insertAndSort(temp, score[i]);
}
answer[i] = temp.get(0);
}
return answer;
}
private void insertAndSort(ArrayList<Integer> list, int value) {
int index = Collections.binarySearch(list, value);
if (index < 0) {
index = -index - 1;
}
list.add(index, value);
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/138477