[프로그래머스, 138477] 명예의 전당 (1) (java)
Problem Solving/Programmers

[프로그래머스, 138477] 명예의 전당 (1) (java)

728x90

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

728x90

메모리: 96.1 MB, 시간: 4.20 ms

사용 알고리즘: 자료 구조

import java.util.*;

class Solution {
    public int[] solution(int k, int[] score) {
        
        // 순위를 담을 우선순위 큐
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        int[] answer = new int[score.length];
        
        for(int i = 0; i < score.length; i++) {
            pq.add(score[i]);
            
            // 우선순위 큐에 k개 초과로 점수가 들어있다면
            // 명예의 전당 순위에서 빠진 점수 하나를 빼줌
            if(pq.size() > k) pq.poll();
            
            // 최하위 점수 기록
            answer[i] = pq.peek();
        }
        
        return answer;
    }
}
728x90