← All Lessons Lesson 67 / 68
Lesson 67

Finding the Busiest Interval: Peak Resource Requirement

Finding the Busiest Interval

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:

  • a start time (s) — when it begins
  • an end time (e) — when it finishes
  • the resources (r) — how much it needs the whole time it runs

So 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.

What are we trying to find?

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:

  1. the time interval that is busiest, and
  2. the maximum total resources needed during that interval.

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.

When do two jobs "overlap"?

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:

  • Job A runs from time 1 to 5.
  • Job B runs from time 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.

The function

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.

How to think about solving it

A common and intuitive idea is to look at the timeline as a series of events:

  • When a job starts, the running total of resources goes up by r.
  • When a job ends, the running total goes down by 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.

Finding the Busiest Interval: Peak Resource Requirement
Diagram — click to zoom (scroll / drag to pan)