Jam's story
[프로그래머스] 명예의 전당 java 본문
public static int[] solution(int k, int[] score) {
int[] answer = new int[score.length];
ArrayList<Integer> arr=new ArrayList<>();
for (int i = 0; i < score.length; i++) {
if(i<k-1) {
arr.add(score[i]);
arr.sort(Collections.reverseOrder());
answer[i]=arr.get(arr.size()-1);
}else if(i>=k-1){
arr.add(score[i]);
arr.sort(Collections.reverseOrder());
answer[i]=arr.get(k-1);
}
}
return answer;
}
다른사람풀이
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;
}
}
queue.poll(); //queue에 가장 작은 데이터를 뽑습니다.
queue.peek(); //queue의 가장 작은 데이터를 확인합니다.
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 배열의 유사도 (0) | 2022.12.29 |
---|---|
[프로그래머스] ox퀴즈 (0) | 2022.12.29 |
[프로그래머스] 숫자찾기 java (0) | 2022.12.27 |
[프로그래머스] 문자열 나누기 java (0) | 2022.12.27 |
[프로그래머스] 자릿수 더하기 java (0) | 2022.12.26 |
Comments