-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskController.java
More file actions
68 lines (61 loc) · 1.64 KB
/
Copy pathDiskController.java
File metadata and controls
68 lines (61 loc) · 1.64 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
66
67
68
package heap;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class DiskController {
public static void main(String[] args) {
SolutionDiskController su = new SolutionDiskController();
// int[][] jobs = { { 0, 3 }, { 1, 9 }, { 2, 6 } }; // 9ms
int[][] jobs = { { 0, 5 }, { 1, 2 }, { 5, 5 } }; // 6ms
// int[][] jobs = {{0, 9}, {0, 4}, {0, 5}, {0, 7}, {0, 3}}; // 13ms
System.out.println(su.solution(jobs));
}
}
class SolutionDiskController {
public int solution(int[][] jobs) {
int answer = 0;
Arrays.sort(jobs, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
PriorityQueue<Work> qu = new PriorityQueue<>();
int len = jobs.length;
int curTime = 0;
int idx = 0;
while(true) {
while(idx < len && jobs[idx][0] <= curTime) {
qu.add(new Work(jobs[idx][0], jobs[idx][1]));
idx++;
}
if(qu.isEmpty()) {
if(idx == len){
break;
}else {
curTime++;
}
}else {
Work tmp = qu.poll();
curTime += tmp.workTime;
answer += curTime - tmp.start;
}
}
return answer / len;
}
}
class Work implements Comparable<Work> {
int start;
int workTime;
Work(int start, int workTime) {
this.start = start;
this.workTime = workTime;
}
public int compareTo(Work o) {
return this.workTime - o.workTime;
}
@Override
public String toString() {
return "Work{" + "start=" + start + ", workTime=" + workTime + "}";
}
}