Find Continuous Peak Resource Load
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Find the maximum aggregate load over a set of half-open intervals and identify the earliest contiguous time window sustaining it.
You are tracking system metrics across multiple tasks. Each task is defined by a triplet [start, end, load], indicating that it contributes load units of resource consumption continuously over the half-open time interval [start, end). That is, the load is active for any time t satisfying start <= t < end.
Your goal is to determine:
- The maximum total resource load active at any moment.
- The earliest contiguous time interval
[peak_start, peak_end)during which this peak load was continuously maintained without dropping.
Rules & Edge Cases
- Intervals are half-open
[start, end): a task starting at timetadds its load att, while a task ending attremoves its load att. If one task ends and another starts at the exact same timestampt, both changes take effect simultaneously att. - If consecutive time segments sustain the peak load without any drop in load in between, they must be merged into one single continuous interval
[peak_start, peak_end). - If the maximum load occurs across multiple separate time windows, return the interval that starts at the earliest time.
- If the input task list is empty, return
(0, 0, 0).
Return the result as a 3-tuple (peak_load, peak_start, peak_end).
Constraints
0 <= len(tasks) <= 2 * 10^50 <= start < end <= 10^91 <= load <= 10^9- Total load values can exceed standard 32-bit signed integers.
Examples
Example 1
Input: find_peak_memory([[1, 5, 10], [2, 4, 20], [3, 6, 15]])
Output: [45, 3, 4]
Example 2
Input: find_peak_memory([[1, 4, 10], [4, 7, 10], [2, 5, 10]])
Output: [20, 2, 5]
Hints
Hint 1
Consider using a line-sweep (difference array) approach by generating event points for task starts (+load) and task ends (-load).
Hint 2
To handle simultaneous starts and ends at the exact same timestamp without creating fake spikes or dips, aggregate all deltas per timestamp before processing.
Hint 3
Iterate over sorted unique timestamps and keep track of the running load on each interval [t_i, t_{i+1}). Merge consecutive intervals if the running load equals the current maximum.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem can be efficiently solved using the Sweep-Line Algorithm (or Event-Based Difference Array):
-
Event Generation & Aggregation: Each process tuple
(start, end, load)represents a net load change of+loadatstartand-loadatend. We aggregate all changes at identical timestamps in a hash map (defaultdict). Aggregating before sorting guarantees that multiple events occurring at the exact same timestamptcancel out or combine immediately, respecting the half-open interval rule. -
Time Discretization: Extract and sort all unique timestamps
t_0 < t_1 < ... < t_k. The resource consumption remains constant within any continuous segment[t_i, t_{i+1}). -
Sweep and Merge Peak Windows: Maintain a running load sum as we step through the sorted unique timestamps:
- Apply the aggregated delta at
t_i. The new sum is the load during[t_i, t_{i+1}). - If
current > peak: updatepeak, setpeak_start = t_i, and setpeak_end = t_{i+1}. - If
current == peakandpeak_end == t_i: the peak level was sustained without interruption across the boundaryt_i. Extend the existing peak interval by updatingpeak_end = t_{i+1}. - Disjoint windows with the same peak load are ignored because
current > peakwill be false andpeak_end == t_iwill fail (since the load fell below peak in the interim).
- Apply the aggregated delta at
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$ where $N$ is the number of processes. There are at most $2N$ timestamps to sort. The sweep pass runs in linear time relative to the number of unique timestamps.
- Space Complexity: $\mathcal{O}(N)$ to store the hash map of events and the sorted timestamp array.
Python Reference Solution
from collections import defaultdict
from typing import List, Tuple
def find_peak_memory(processes: List[List[int]]) -> Tuple[int, int, int]:
if not processes:
return (0, 0, 0)
events = defaultdict(int)
for start, end, memory in processes:
events[start] += memory
events[end] -= memory
times = sorted(events)
current = 0
peak = 0
peak_start = 0
peak_end = 0
for i in range(len(times) - 1):
time = times[i]
current += events[time]
next_time = times[i + 1]
if current > peak:
peak = current
peak_start = time
peak_end = next_time
elif current == peak and current > 0 and peak_end == time:
peak_end = next_time
return (peak, peak_start, peak_end)