You are given an array intervals where intervals[i] = [start_i, end_i, resources_i]. Each interval is a task that holds resources_i units of a shared resource during the half-open time range [start_i, end_i), meaning it is active at every instant t with start_i <= t < end_i.
Return the maximum total resource demand at any single instant, which is the largest sum of resources over all intervals active at the same time.
Function Signature
def peak_resource_usage(intervals: list[list[int]]) -> int:
Rules
-
An interval ending at time
t
is no longer active at
t
, so it does not overlap an interval starting at
t
.
-
The input may be unsorted.
-
Duplicate intervals each add their own resources independently.
Constraints
-
1 <= len(intervals) <= 10^5
-
len(intervals[i]) == 3
-
0 <= start_i < end_i <= 10^9
-
1 <= resources_i <= 10^9
-
The answer fits in a 64-bit integer (at most
10^14
).
Examples
Example 1
Input: intervals = [[1,4,3],[3,5,2],[4,6,4]]
Output: 6
At t = 3 the active demand is 3 + 2 = 5. At t = 4 the first interval has ended (half-open), so the demand is 2 + 4 = 6, and no instant exceeds 6.
Example 2
Input: intervals = [[5,10,7],[0,20,3],[6,8,4],[12,15,10]]
Output: 14
The input is unsorted and nested. During [6, 8) the intervals [0,20), [5,10) and [6,8) are all active, giving 3 + 7 + 4 = 14, while during [12, 15) the demand is only 3 + 10 = 13.
Example 3
Input: intervals = [[1,3,5],[3,6,5],[3,6,5]]
Output: 10
The first interval ends at 3, exactly when the other two start, so it never overlaps them. The two identical intervals count separately and give 5 + 5 = 10 during [3, 6).