본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 명예의 전당 (1)

by 현장 2025. 2. 26.

-Code

import java.util.*;

class Solution {
    public int[] solution(int k, int[] score) {
        List<Integer> honor = new ArrayList<>();
        List<Integer> answer = new ArrayList<>();
        for (int p : score) {
           honor.add(p);
           Collections.sort(honor);
           if (honor.size() > k) {
               honor.remove(0);
           }
           answer.add(honor.get(0));
        }

        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}