본문 바로가기
알고리즘 & 자료구조/프로그래머스

명예의 전당 (1)

by 신재권 2022. 12. 16.
package programmers;

import java.util.Arrays;
import java.util.PriorityQueue;

public class 명예의전당1 {

   public static 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.poll();
         }
         answer[i] = q.peek();
      }
      return answer;
   }

   public static void main(String[] args) {
      System.out.println(Arrays.toString(solution(3, new int[] {10, 100, 20, 150, 1, 100, 200})));
      System.out.println(Arrays.toString(solution(4, new int[] {0, 300, 40, 300, 20, 70, 150, 50, 500, 1000})));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

옹알이 (2)  (0) 2022.12.21
기사단원의 무기  (0) 2022.12.20
과일 장수  (0) 2022.12.14
가장 가까운 같은 글자  (0) 2022.12.13
정수 삼각형  (0) 2022.12.04