-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1834.single-threaded-cpu.java
More file actions
65 lines (54 loc) · 1.82 KB
/
Copy path1834.single-threaded-cpu.java
File metadata and controls
65 lines (54 loc) · 1.82 KB
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
55
56
57
58
59
60
61
62
63
64
65
class Solution {
public int[] getOrder(int[][] tasks) {
int[][] taskswithId = new int[tasks.length][3];
for(int i=0;i<tasks.length;i++){
taskswithId[i] = new int[]{tasks[i][0],tasks[i][1], i};
// if(i == 35079 || i == 4551)
// System.out.println(i+" "+tasks[i][0]+" "+tasks[i][1]);
}
Arrays.sort(taskswithId, new Comparator<int[]>(){
@Override
public int compare(int[] t1, int[] t2){
if(t1[0] == t2[0])
return t1[2]-t2[2];
return t1[0]-t2[0];
}
});
PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>(){
@Override
public int compare(int[] t1, int[] t2){
if(t1[1] == t2[1])
return t1[2]-t2[2];
return t1[1]-t2[1];
}
});
int time = taskswithId[0][0];
int i = 0;
int idletime = 0;
int j=0;
int[] res = new int[tasks.length];
while( i<taskswithId.length){
while(i<taskswithId.length && time >= taskswithId[i][0]){
pq.offer(taskswithId[i]);
i++;
}
if(pq.isEmpty())
time = taskswithId[i][0];
else
{
int[] curr = pq.poll();
time += curr[1];
res[j] = curr[2];
j++;
}
// time++;
}
while(!pq.isEmpty()){
res[j++] = pq.poll()[2];
}
return res;
}
}
//Storage task id in another datastructure
//sort with respect to enq time
//have priority queue ordered by processingTime