알고리즘/프로그래머스
[프로그래머스] 프린터_자바
Ellie67
2022. 11. 30. 09:45
https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
우선 순위 큐를 사용해야 하는 문제
priorities 배열을 우선 순위 큐에 넣으면 오름차순으로 정렬 되기 때문에
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
이렇게 내림차순으로 정렬한다.
import java.util.*;
public class P4 {
public static void main(String[] args){
P4 p = new P4();
int[] priorities = {2, 1, 3, 2};
int location = 2;
System.out.println(p.solution(priorities,location));
}
public int solution(int[] priorities, int location) {
int answer = 0;
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
for(int i=0; i<priorities.length; i++){
q.add(priorities[i]);
}
while(!q.isEmpty()){
for(int i=0; i<priorities.length; i++){
if(q.peek() == priorities[i]){
if(i == location){
answer++;
return answer;
}
q.poll();
answer++;
}
}
}
return answer;
}
}