package programmers;
import java.util.Collections;
import java.util.PriorityQueue;
public class 프린터_230101 {
public static void main(String[] args) {
int [] priorities = {1, 1, 9, 1, 1, 1};
int location = 0;
System.out.println(solution(priorities, location));
}
public static int solution(int[] priorities, int location) {
int answer = 0;
int len = priorities.length;
PriorityQueue <Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i < len; i++){
queue.add(priorities[i]);
}
while(queue.isEmpty() == false){
for(int i = 0; i < len; i++){
if(priorities[i] == queue.peek()){
//System.out.println("" + priorities[i] + " " + i);
if(i == location){
answer++;
return answer;
}
queue.poll();
answer++;
}
}
}
answer = -1; //초기값을 -1하고 ++해도 되겠지만...
return answer;
}
}
답안에 빨리 실행된다는 코드도 같이 공부 해뒀음!
↓
댓글
댓글 쓰기