-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProblem_6_MaximumCPULoad.java
More file actions
38 lines (33 loc) · 1.18 KB
/
Problem_6_MaximumCPULoad.java
File metadata and controls
38 lines (33 loc) · 1.18 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
package Merge_Intervals;
// Problem Statement: Maximum CPU Load (hard)
// LeetCode Question: 1235. Maximum Profit in Job Scheduling
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
public class Problem_6_MaximumCPULoad {
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 int findMaxCPULoad(List<Job> jobs){
Collections.sort(jobs, (a, b) -> Integer.compare(a.start, b.start));
int maxCPULoad = 0;
int currentCPULoad = 0;
PriorityQueue<Job> minHeap = new PriorityQueue<>(jobs.size(), (a,b) -> Integer.compare(a.end, b.end));
for (Job job : jobs){
while(!minHeap.isEmpty() && job.start > minHeap.peek().end){
currentCPULoad -= minHeap.poll().cpuLoad;
}
minHeap.offer(job);
currentCPULoad += job.cpuLoad;
maxCPULoad = Math.max(maxCPULoad, currentCPULoad);
}
return maxCPULoad;
}
}
}