42587 - 프로세스
info
- 문제 보기: 42587 - 프로세스
- 소요 시간: 12분 38초
- 풀이 언어:
java
- 체감 난이도: 2️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
큐
정렬
풀이 코드
info
- 메모리: 95700 KB
- 시간: 2 ms
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
PriorityQueue<Process> pq = new PriorityQueue<>((a, b) -> b.priority - a.priority);
Deque<Process> dq = new ArrayDeque<>();
for (int i = 0; i < priorities.length; ++i) {
Process p = new Process(i, priorities[i]);
pq.add(p);
dq.add(p);
}
int ans = 1;
while (true) {
Process p = dq.pop();
if (p.priority == pq.peek().priority) {
if (p.id == location) return ans;
else {
pq.poll();
++ans;
}
}
else {
dq.add(p);
}
}
}
class Process {
int id;
int priority;
Process(int id, int priority) {
this.id = id;
this.priority = priority;
}
}
}