Imagine you run a small data center. Many jobs want to run, and each job needs a certain amount of resources (think CPU power or memory) while it is running. Each job has three pieces of information:
s) — when it beginse) — when it finishesr) — how much it needs the whole time it runsSo a job looks like [s, e, r], and the full input is a list of jobs:
[[s1, e1, r1], [s2, e2, r2], ...]
One important rule: for every job, si < ei. A job always starts before it ends — that just makes sense.
Sometimes jobs run at the same time. When two or more jobs overlap, their resource needs add up during that shared period. The moment when the most resources are needed all at once is the busiest interval.
Our task is to find:
Think of it like a parking garage. Cars (jobs) come and go. At any moment, the cars present share the garage. We want to find the time window when the garage is most crowded — and how many cars are inside then.
This is the key detail, and it is easy to get wrong. Two intervals [s1, e1] and [s2, e2] overlap if:
e1 > s2
In plain words: the first job must still be running after the second job has already started.
There is a special edge case. If one job ends at the exact moment another starts — that is, e1 == s2 — they are not overlapping. One has fully finished before the other begins. There is no shared time, just a touching point.
A simple example:
1 to 5.5 to 8.Here e1 == s2 (both are 5), so A and B do not overlap. A is done exactly when B starts.
But if Job B ran from 4 to 8 instead, then e1 (5) > s2 (4), so they do overlap between time 4 and 5. During that shared window, you would add both jobs' resources together.
You are asked to write a function that takes the jobs and returns the answer:
class Solution {
public:
vector<int> peakResourceRequirement(vector<...> jobs) {
// ...
}
};
The output should describe both the busiest interval and the maximum resources needed during it.
A common and intuitive idea is to look at the timeline as a series of events:
r.r.If you sort all these events by time and walk through them in order, the running total tells you exactly how busy each moment is. Keep track of the highest total you ever see — that is your peak resource requirement, and the time window where it happens is your busiest interval. Remember the rule about e1 == s2: a job ending should free its resources before a new job starting at that same time adds its own.