프로그래머스 1) 명예의 전당

유병수·2023년 5월 2일

명예의전당(1)

우선순위큐를 쓰면 간단히 해결되는 문제.
처음에 java에 우선순위큐가 있는지 몰라서 deque로 풀려고 했으나 너무 복잡해서 python으로 풀어보고 나서 사람들 코드를 보고나서 java에도 우선순위 큐가 있는걸 알고 적용

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 i=0; i<score.length; i++){
            
            pq.add(score[i]);
            
            if(pq.size() > k){
                pq.poll();
            }
            
            answer[i] = pq.peek();
        }
        return answer;
    }
}

0개의 댓글