Richest Window of Stalls Under Count and Distance Limits
Arrays & HashingAuto-graded · 20 test casesLast updated
Find the best sum over a contiguous run of position-sorted items, limited by item count and coordinate span, using sorting and a two-pointer sweep.
Richest Window of Stalls Under Count and Distance Limits
A market has several stalls laid out along a line. Each stall is described by a pair [position, coins]: where it stands and how many coins it holds. The list is not necessarily sorted, and several stalls may share a position.
You may take coins from a run of stalls. To define a run, first sort all stalls by position in non-decreasing order. A run is any group of stalls that sits next to each other in that sorted order, meaning a contiguous block of the sorted list. A run is allowed if both conditions hold:
- It contains at most
kstalls. - The position of its last stall minus the position of its first stall is at most
d.
Return the largest total number of coins you can collect from a single allowed run.
Notes
- A run with a single stall is always allowed.
- Stalls with equal positions may appear in any relative order after sorting. This does not change the answer.
- Do not modify the input list.
Function Signature
def richest_window(stalls: List[List[int]], k: int, d: int) -> int:
Constraints
1 <= len(stalls) <= 10^50 <= position <= 10^90 <= coins <= 10^41 <= k <= len(stalls)0 <= d <= 10^9
Examples
Example 1
Input: richest_window([[1, 10], [2, 20], [5, 30], [6, 40]], 2, 2)
Output: 70
Example 2
Input: richest_window([[1, 5], [10, 5], [20, 5]], 2, 5)
Output: 5
Hints
Hint 1
After sorting by position, every allowed selection is a contiguous subarray, so think in terms of windows.
Hint 2
Coins are never negative, so for a fixed right end, the longest allowed window is never worse than a shorter one.
Hint 3
Use two pointers: add the right stall, then advance the left pointer while the window has more than k stalls or spans more than d. Keep a running sum and track the maximum.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort a copy of the stalls by position. Every allowed selection is then a contiguous window [left, right] of the sorted list that satisfies:
- the count:
right - left + 1 <= k - the span:
pos[right] - pos[left] <= d
Sweep right from left to right, adding each stall's coins to a running total. While the window violates either limit, remove the stall at left from the total and advance left. After the window becomes valid, update the best total seen.
Why it is correct
- Both limits are monotone: if a window is allowed, every shorter window with the same right end is also allowed. Also, as
rightincreases, the smallest validleftnever moves backward, so the left pointer only needs to move forward. - Coins are non-negative, so for a fixed right end the longest allowed window has the largest sum. The sweep finds exactly that window for each
right. - Every allowed window is dominated by the longest allowed window sharing its right end, so taking the maximum over all right ends gives the answer.
- The shrinking loop always terminates, because when
left == rightthe count is 1 (k >= 1) and the span is 0 (d >= 0).
Duplicate positions and d = 0 are handled naturally: stalls at the same position have span 0 and can be grouped, up to k of them.
Complexity
- Time:
O(n log n)for sorting, plusO(n)for the sweep since each index enters and leaves the window at most once. - Space:
O(n)for the sorted copy.
Reference solution
from typing import List
def richest_window(stalls: List[List[int]], k: int, d: int) -> int:
ordered = sorted(stalls, key=lambda s: s[0])
left = 0
total = 0
best = 0
for right, (position, coins) in enumerate(ordered):
total += coins
while right - left + 1 > k or position - ordered[left][0] > d:
total -= ordered[left][1]
left += 1
if total > best:
best = total
return best