-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaxCPULoad.java
68 lines (59 loc) · 2.14 KB
/
MaxCPULoad.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
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.*;
/**
* @author kalpak
*
* Given an array of jobs with different time requirements, where each job consists of start time, end time and CPU load.
* The task is to find the maximum CPU load at any time if all jobs are running on the same machine.
*
* Examples:
*
* Input: jobs[] = {{1, 4, 3}, {2, 5, 4}, {7, 9, 6}}
* Output: 7
* Explanation:
* In the above-given jobs, there are two jobs which overlaps.
* That is, Job [1, 4, 3] and [2, 5, 4] overlaps for the time period in [2, 4]
* Hence, the maximum CPU Load at this instant will be maximum (3 + 4 = 7).
*
* Input: jobs[] = {{6, 7, 10}, {2, 4, 11}, {8, 12, 15}}
* Output: 15
* Explanation:
* Since, There are no jobs that overlaps.
* Maximum CPU Load will be – max(10, 11, 15) = 15
*/
class Job {
int start;
int end;
int cpuLoad;
public Job(int start, int end, int cpuLoad) {
this.start = start;
this.end = end;
this.cpuLoad = cpuLoad;
}
};
public class MaxCPULoad {
public static int findMaxCPULoad(List<Job> jobs) {
int result = 0;
int currentCPULoad = 0;
PriorityQueue<Job> minHeap = new PriorityQueue<>((a, b) -> a.end - b.end);
Collections.sort(jobs, (a, b) -> a.start - b.start);
for (Job job : jobs) {
while(!minHeap.isEmpty() && job.start > minHeap.peek().end) {
currentCPULoad -= minHeap.peek().cpuLoad;
minHeap.poll();
}
currentCPULoad += job.cpuLoad;
result = Math.max(result, currentCPULoad);
minHeap.offer(job);
}
return result;
}
public static void main(String[] args) {
List<Job> input = new ArrayList<Job>(Arrays.asList(new Job(1, 4, 3), new Job(2, 5, 4), new Job(7, 9, 6)));
System.out.println(findMaxCPULoad(input));
input = new ArrayList<Job>(Arrays.asList(new Job(6, 7, 10), new Job(2, 4, 11), new Job(8, 12, 15)));
System.out.println(findMaxCPULoad(input));
input = new ArrayList<Job>(Arrays.asList(new Job(1, 4, 2), new Job(2, 4, 1), new Job(3, 6, 5)));
System.out.println(findMaxCPULoad(input));
}
}