-
프로그래머스 - 명예의 전당알고리즘 2024. 4. 17. 01:08
import java.util.*; class Solution { public int[] solution(int k, int[] score) { int[] answer = new int[score.length]; PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(); int temp = 0; for(int i = 0; i < score.length; i++) { priorityQueue.add(score[i]); if (priorityQueue.size() > k) { priorityQueue.poll(); } answer[i] = priorityQueue.peek(); } return answer; } }
명예의 전당 목록의 k 순위의 가장 최하위 점수를 찾는 문제입니다. 자바의 우선순위 큐를 활용해, 각 점수를 큐에 넣습니다.
이 때, 큐의 크기가 k를 넘었다면 가장 낮은 점수(우선순위가 높은)을 제거 후 가장 낮은 점수를 반환하여 answer 배열에 값을 넣습니다.
'알고리즘' 카테고리의 다른 글
프로그래머스 - 덧칠하기 (0) 2024.04.29 프로그래머스 - 모의고사 (0) 2024.04.21 프로그래머스 - 콜라 문제 (0) 2024.04.14 프로그래머스 - 푸드 파이트 대회 (0) 2024.04.13 프로그래머스 - 두 개 뽑아서 더하기 (0) 2024.04.13