We are given a list of Jobs. Each job has a Start time, an End time, and a CPU load when it is running. Our goal is to find the maximum CPU load at any time if all the jobs are running on the same machine.
Example 1:
1
2
3
4
Input: jobs =[[1,4,3],[2,5,4],[7,9,6]]Output: 7Explanation: Since [1,4,3] and [2,5,4] overlap, their maximum CPU load(3+4=7) will be when both the
jobs are running at the same time i.e., during the time interval(2,4).
Example 2:
1
2
3
Jobs: [[6,7,10],[2,4,11],[8,12,15]]Output: 15Explanation: None of the jobs overlap, therefore we will take the maximum load of any job which is15.
Example 3:
1
2
3
Jobs: [[1,4,2],[2,4,1],[3,6,5]]Output: 8Explanation: Maximum CPU load will be 8 as all jobs overlap during the time interval [3,4].
Similarly, for Maximum CPU Load, we need to determine the peak number of concurrent jobs and maintain a running total of the CPU load over time to identify the maximum load encountered.
Here is the approach:
Sort Events: Convert job intervals into start and end events, and sort these events by start time.
Track CPU Load: As we iterate through these jobs, we track overlaps incrementally as done previously. We use a min heap to keep track of current jobs and their loads. Add the load when a job starts and subtract the load when a job ends.
Calculate Maximum Load: Maintain a running total of the current CPU load and update the maximum load encountered during the process.