Leftmost Busiest Slot After Range Increments
Arrays & HashingAuto-graded · 25 test casesLast updated
Apply many inclusive range additions over a possibly huge array and return the index holding the largest total, breaking ties toward the smallest index.
A row of n slots, numbered 0 through n - 1, each start with a value of 0. You are given a list of m range updates. Each update is a triple [left, right, amount] meaning: add amount to every slot whose index lies in the inclusive range [left, right].
An amount may be positive, zero, or negative, and slots that no update touches keep a value of 0.
After all updates have been applied, report the index of the slot holding the largest final value. If several slots tie for the largest value, report the smallest such index.
The parameters arrive in this order: the slot count n, then the list of updates in the order they were issued. Only the final state matters for the answer — the order of updates does not affect the result.
Example walkthrough: with n = 5 and updates [[1, 3, 2], [2, 4, 3]], the slot values end up as [0, 2, 5, 5, 3]. The largest value is 5, reached at indices 2 and 3, so the answer is 2.
Constraints
1 <= n <= 10^6- The number of updates may be
0or more, withm <= 10^5 0 <= left <= right < nfor every updateamountis any integer, possibly negative or zero- The answer is derived only from the final values; do not rely on any particular update order
- A correct solution should run in time roughly proportional to
n + m, notn * m - If no updates are given, every slot is
0and the answer is0
Examples
Example 1
Input: busiest_slot_index(5, [[1, 3, 2], [2, 4, 3]])
Output: 2
Example 2
Input: busiest_slot_index(5, [[1, 3, 2], [2, 4, 3]])
Output: 2
Example 3
Input: busiest_slot_index(4, [[0, 3, -2]])
Output: 0
Hints
Hint 1
Applying each update slot by slot is too slow because a single update can touch the whole row — look for a way to record only where each update begins and ends.
Hint 2
Store `+amount` at `left` and `-amount` just past `right`, then walk the row left to right accumulating a running total to recover each slot's real value.
Hint 3
Track the best value and its index as you accumulate; use a strict 'greater than' comparison so the first (smallest) index wins ties, and initialize from the first real value so all-negative data works.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Applying each update element by element is too slow: an update may span the entire row, giving worst-case work of order n * m. Instead, record each update as a pair of boundary events in a difference array of size n + 1:
- Add
amountat positionleft, marking where the increment takes effect. - Add
-amountat positionright + 1, marking where it stops (the extra slot keeps that index in bounds whenright == n - 1).
Each update then costs constant time. A single left-to-right pass accumulates the events: the running sum after position i equals the actual final value of slot i, because every update contributes its amount to exactly the positions it covers and cancels itself out everywhere else. While accumulating, keep the largest value seen and its index, updating only on a strict increase. Since the scan moves left to right and never replaces the record on a tie, the earliest index among equal maxima is retained.
Starting the best value from the first computed slot (rather than a sentinel like 0) ensures correctness when every slot ends up negative.
Correctness
For any position i and any single update (left, right, amount), the prefix sum of that update's two events at i is amount when left <= i <= right (the start event is included and the stop event is not) and 0 otherwise. Summing over all updates, the running total at i equals the final value of slot i. The scan therefore examines every final value, and choosing the first strict maximum yields the smallest index among ties.
Complexity
- Time:
O(m)to record the events plusO(n)to accumulate —O(n + m)overall. - Space:
O(n)for the difference array.
Reference solution
from typing import List
def busiest_slot_index(n: int, updates: List[List[int]]) -> int:
diff = [0] * (n + 1)
for left, right, amount in updates:
diff[left] += amount
diff[right + 1] -= amount
best_index = 0
best_value = None
running = 0
for i in range(n):
running += diff[i]
if best_value is None or running > best_value:
best_value = running
best_index = i
return best_index