제출 코드
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) throws Exception {
int answer = 0;
Queue<Process> que = new LinkedList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i < priorities.length; i++) {
que.add(new Process(priorities[i], i));
pq.add(priorities[i]);
}
int count = 0;
while(!que.isEmpty()) {
Process current = que.poll();
if(current.priority == pq.peek()) {
pq.remove();
count++;
if(current.index == location) {
answer = count;
break;
}
}
else {
que.add(current);
}
}
return answer;
}
static class Process {
private int priority;
private int index;
public Process(int priority, int index) {
this.priority = priority;
this.index = index;
}
public String toString() {
return "{priority: " + priority + ", index: " + index + "}";
}
}
}
문제 의도
- 큐의 기본 동작 이해
- 시뮬레이션
'알고리즘' 카테고리의 다른 글
백준 11279번 | 최대 힙 문제 풀이 (Java) (0) | 2025.02.19 |
---|---|
프로그래머스 42586번 | 기능개발 문제 풀이 (Java) (0) | 2025.02.18 |
백준 1966번 | 프린터큐 문제 풀이 (Java) (0) | 2025.02.16 |
백준 2164번 | 카드2 문제 풀이 (Java) (0) | 2025.02.13 |
프로그래머스 12909번 | 올바른 괄호 문제 풀이 (Java) (0) | 2025.02.12 |