Deep_Dev
article thumbnail

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 


์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆฐ๋‹ค.

Queue์— score๋ฅผ ํ•˜๋‚˜์”ฉ ๋„ฃ์œผ๋ฉด์„œ, Queue์˜ size๊ฐ€ k๋ฅผ ๋„˜์–ด๊ฐˆ๋•Œ๋ถ€ํ„ฐ๋งŒ ์‚ญ์ œ์ฃผ๋ฉด์„œ ์ฒซ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

import java.util.*;
class Solution {
    public int[] solution(int k, int[] score) {
        int[] answer = new int[score.length];

        PriorityQueue<Integer> Q = new PriorityQueue<>();

        for(int i=0; i<score.length; i++){
            Q.add(score[i]);
            if(Q.size()>k){
                Q.remove();
            }
            answer[i]=Q.peek();
        }
        return answer;
    }
}