Wondering if the heap is necessary? I am merging overlapping intervals and keeping track of maxSoFar. This seems to work with the given use cases, but not sure if I might be missing some other important corner case? Any inputs would be welcomed.
public static int findMaxCPULoad(List<Job> jobs) {
Collections.sort(jobs, Comparator.comparing(e -> e.start));
Job interval1 = jobs.get(0);
int maxSoFar = interval1.cpuLoad;
for (int i = 1; i < jobs.size(); i++) {
Job interval2 = jobs.get(i);
if (interval2.start <= interval1.end) { // In case of overlap, find max load and calculate next interval
interval1 = new Job(Math.min(interval1.start, interval2.start), Math.max(interval1.end, interval2.end), interval1.cpuLoad + interval2.cpuLoad);
maxSoFar = Math.max(maxSoFar, interval1.cpuLoad);
} else {
maxSoFar = Math.max(maxSoFar, interval2.cpuLoad);
interval1 = interval2;
}
}
return maxSoFar;
}