-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinter.java
38 lines (28 loc) · 897 Bytes
/
Printer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
class Job{
int id;
int priority;
public Job(int id, int priority){this.id = id; this.priority = priority;}
}
class Solution {
public static int solution(int[] priorities, int location) {
int priority = priorities.length-1;
Queue<Job> printerList = new LinkedList<>();
for(int i=0; i<priorities.length; i++)
printerList.add(new Job(i,priorities[i]));
Arrays.sort(priorities);
int answer = 1;
while(!printerList.isEmpty()){
if(printerList.peek().priority >= priorities[priority]){
Job curJob = printerList.poll();
if(curJob.id == location) return answer;
answer++;
priority--;
}
else{
printerList.add(printerList.poll());
}
}
return answer;
}
}