Assay Peak Load Span
Longest interval with a bounded sum of its largest k values
A genetics lab has an ordered strip of assay sites. The nonnegative integer loads[i] is the load measured at site i.
For any nonempty contiguous interval, its peak load is the sum of its k largest measurements. If the interval contains fewer than k sites, its peak load is the sum of all its measurements. Equal measurements at different sites count separately.
The lab can process an interval only when its peak load is at most budget.
Return the inclusive, zero-based endpoints [left, right] of the longest interval the lab can process. If several intervals have the same maximum length, return the one with the smallest left. If no nonempty interval can be processed, return [].
Examples
Example 1
Input: loads = [4,1,5,2,3], k = 2, budget = 8 Output: [1,4]
The interval from index 1 through index 4 has measurements [1, 5, 2, 3]. Its two largest measurements sum to 8. The full strip has peak load 9, so it is not affordable.
Example 2
Input: loads = [2,0,0,1,0], k = 3, budget = 0 Output: [1,2]
With a zero budget, only intervals made entirely of zeros are affordable. The consecutive zeros at indices 1 and 2 form the longest such interval.
Example 3
Input: loads = [6,7], k = 1, budget = 5 Output: []
For k = 1, an interval's peak load is its largest measurement. Both individual measurements exceed the budget, so no nonempty interval is affordable.
Constraints
- 1 <= loads.length <= 100000
- 0 <= loads[i] <= 1000000000
- 1 <= k <= loads.length
- 0 <= budget <= 100000000000000
The reference algorithm uses O(n log n) time and O(n) auxiliary space. Lazy heap entries are identified by their original indices. All possible sums fit exactly in JavaScript's safe integer range.
Hints
Show hint 1Hint 1
Extending an interval cannot decrease the sum of its largest k measurements. What does this imply about moving the left endpoint?
Show hint 2Hint 2
Separate the current interval into its largest k measurements and the remaining measurements. Maintain the first group's sum, and use two heaps with index-based lazy deletion to move measurements between the groups.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you simplify the algorithm when k = 1 or when k equals the length of the entire input?
- Can you support a stream of measurements while retaining only O(w) heap entries, where w is the current window length, by periodically rebuilding the heaps?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem