Skip to content

Commit be745b2

Browse files
committed
[PGS] 프로세스 / Level2 (Java 풀이) / 52분 / 실패
1 parent f164643 commit be745b2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int[] priorities, int location) {
5+
// 1) 작업 큐: 인덱스(idx)와 우선순위(priority)를 함께 보관
6+
Queue<DTO> queue = new LinkedList<>();
7+
8+
// 2) 우선순위 큐: 남아있는 작업 중 가장 높은 우선순위를 꺼내기 위함
9+
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
10+
11+
for (int i = 0 ; i < priorities.length; i++) {
12+
queue.offer(new DTO(i, priorities[i]));
13+
pq.offer(priorities[i]);
14+
}
15+
16+
// System.out.println(queue);
17+
// System.out.println(pq);
18+
19+
int count = 0; // 실행된 작업 수
20+
21+
while(!queue.isEmpty()) {
22+
DTO cur = queue.poll(); // 현재 작업 꺼냄
23+
24+
// 현재 작업 우선순위가 남은 작업의 최고 우선순위와 같다면
25+
if(cur.priority == pq.peek()) {
26+
count++;
27+
pq.poll(); // 우선순위 큐에서 제거
28+
// 내가 찾던 작업이라면 실행 순서 반환
29+
if (cur.idx == location) {
30+
return count;
31+
}
32+
}
33+
else {
34+
// 우선순위가 더 높은 작업이 남아있다면 뒤로 재삽입
35+
queue.offer(cur);
36+
}
37+
}
38+
return count;
39+
}
40+
41+
// 인덱스 + 우선순위를 함께 보관할 DTO
42+
static class DTO {
43+
int idx;
44+
int priority;
45+
DTO(int idx, int priority) {
46+
this.idx = idx;
47+
this.priority = priority;
48+
// System.out.println("idx: "+idx+" priority: "+priority);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)