출처: https://bumcrush.tistory.com/182 [맑음때때로 겨울]
반응형

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

package Programmers;
import java.util.PriorityQueue;
public class 명예의전당 {
public static void main(String[] args){
명예의전당 T = new 명예의전당();
int k = 3;
int[] score = {10, 100, 20, 150, 1, 100, 200};
T.solution(k, score);
}
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++) {
if(pq.size() < k){
pq.offer(score[i]);
}else{
if(pq.peek() < score[i]){
pq.poll();
pq.offer(score[i]);
}
}
answer[i] = pq.peek();
}
return answer;
}
}

1.  풀이방법

  - PriorityQueue 사용

  - score 배열만큼 돌면서 pq 의 크기가 k보다 작으면 추가하고 , 아닐시 현재의 스코어랑 pq의 제일 작은 값이랑 비교한다.

   (참일시) pq에 들어있는 제일작은 수를 빼고 현재 score을 추가한다. 

 - pq의 제일 작은 값을 answer 에 집어넣는다.

반응형

+ Recent posts