From 201bc6d62d440847dbdff8012dddbc695a068639 Mon Sep 17 00:00:00 2001 From: rl544 <111494264+rl544@users.noreply.github.com> Date: Thu, 1 Dec 2022 16:46:06 +0900 Subject: [PATCH] =?UTF-8?q?updated=20at=202022.=2012.=201.=20=EC=98=A4?= =?UTF-8?q?=ED=9B=84=204:46:06?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/명예의 전당 (1)/solution.java | 32 ++++++++--------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/programmers/명예의 전당 (1)/solution.java b/programmers/명예의 전당 (1)/solution.java index e356f5d..cbdfcae 100644 --- a/programmers/명예의 전당 (1)/solution.java +++ b/programmers/명예의 전당 (1)/solution.java @@ -1,27 +1,17 @@ // [문제 링크]: https://school.programmers.co.kr/learn/courses/30/lessons/138477 -import java.util.ArrayList; -import java.util.Collections; +import java.util.*; + class Solution { public int[] solution(int k, int[] score) { - int[] answer2 = {}; - ArrayList answer = new ArrayList(); - ArrayList hall = new ArrayList(); - for(int i = 0; i < score.length; i++){ - if(i > k-1){ - if(score[i] > hall.get(0)){ - hall.add(score[i]); - hall.remove(hall.get(0)); - } - } - else hall.add(score[i]); - Collections.sort(hall); - answer.add(hall.get(0)); - } - answer2 = new int[answer.size()]; - for (int i = 0; i < answer.size(); i++) { - answer2[i] = answer.get(i); - } - return answer2; + int l = score.length; + int[] answer = new int[l]; + PriorityQueue que1 = new PriorityQueue<>(); + for (int i = 0; i < l; i++){ + que1.add(score[i]); + if(que1.size() > k) que1.poll(); + answer[i] = que1.peek(); + } + return answer; } } \ No newline at end of file