-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler
54 lines (54 loc) · 1.13 KB
/
Scheduler
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
class Task{
int start;
int duration;
Task(int s, int d){
start = s;
duration = d;
}
}
public class Scheduler{
public int getwait(int [] start, int [] duration){
PriorityQueue<Task> que = new PriorityQueue(10,new Comparator<Task>(){
public int compare(Task t1, Task t2){
if(t1.duration<t2.duration) return -1;
else if(t1.duration>t2.duration) return 1;
else return t1.start-t2.start;
}
});
int wait = 0,end = 0,i = 0;
while(i<start.length){
if(start[i]<end){
Task t = new Task(start[i],duration[i]);
que.offer(t);
i++;
}
else if(start[i]==end){
end+=duration[i];
}
else{
if(!que.isEmpty()){
Task t = que.poll();
wait += (end-t.start);
end += t.duration;
}
else{
end = start[i]+duration[i];
i++;
}
}
}
while(!que.isEmpty()){
Task t = que.poll();
wait += (end-t.start);
end += t.duration;
}
return wait;
}
public static void main(String args[]){
Scheduler scheduler = new Scheduler();
int [] start = {2,3,4,5};
int [] duration ={4,5,1,3};
System.out.println(scheduler.getwait(start,duration));
}
}