Closest Bakery Stall for Every Shopper on a Street
Arrays & HashingAuto-graded · 25 test casesLast updated
For each shopper on a line, find the closest stall index, with ties going to the lower index, testing two-sided linear scans for nearest-neighbor lookups.
Closest Stall for Every Shopper
A street is modeled as a row of numbered slots, given as an integer list street. Each slot contains one of three values:
0: the slot is vacant.1: a shopper is standing in the slot.2: a stall selling cake is in the slot.
A slot holds at most one thing. The distance between slots i and j is abs(i - j). Vacant slots, other shoppers and other stalls never block anyone, so only the slot numbers matter.
For every shopper, determine which stall is closest to them and report that stall's slot index.
Rules
- A stall can serve any number of shoppers, so several shoppers may be given the same stall.
- If two stalls are at the same minimal distance from a shopper, choose the one with the smaller index.
- Return a list with one entry per shopper, ordered by the shopper's slot from left to right. Each entry is the slot index of the chosen stall (not a distance).
Constraints
2 <= len(street) <= 10^5- Each
street[i]is0,1or2. streetcontains at least one1and at least one2.
Examples
Example 1
Input: closest_stalls([2, 1, 0, 1, 0, 0, 2, 1])
Output: [0, 0, 6]
Example 2
Input: closest_stalls([1, 1, 0, 2])
Output: [3, 3]
Example 3
Input: closest_stalls([0, 2, 1, 2, 0])
Output: [1]
Hints
Hint 1
For a given shopper, only two stalls can possibly be the best: the nearest one on the left and the nearest one on the right. Anything further out on either side is strictly worse.
Hint 2
Precompute, for every slot, the index of the closest stall at or before it using a left-to-right pass, and the closest stall at or after it using a right-to-left pass. Use a sentinel such as -1 for 'none yet'.
Hint 3
When comparing the two candidates, handle a missing side first, then pick the left stall when the distances are equal, since it always has the smaller index.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
On a line, the closest stall to a shopper at slot i is always one of two candidates: the nearest stall to its left or the nearest stall to its right. Any stall further out on the same side is strictly farther away. So there is no need to compare each shopper against every stall.
- Sweep left to right, remembering the most recent stall index. Store it for every slot as
prev_stall(-1if no stall has been seen). - Sweep right to left in the same way to build
next_stall. - Walk the street left to right. At each shopper, look at the two candidates:
- If there is no stall on the left, take the right one.
- If there is no stall on the right, take the left one.
- Otherwise, take the left stall if
i - left <= right - i, else the right stall.
Why it is correct
- Candidates are complete: for a stall further left than
prev_stall[i], its distance is larger than that ofprev_stall[i], so it can never win. The same holds on the right. - Sweep invariants: after processing slot
i, the forward sweep holds the largest stall index<= i, and the backward sweep holds the smallest stall index>= i. A shopper's slot is never a stall, so the left candidate is strictly less thaniand the right is strictly greater. - Ties: when both distances are equal, the left stall has the smaller index, and the
<=comparison selects it. - Missing sides: the constraints guarantee at least one stall, so at least one candidate always exists.
- Order: the final pass scans slots in increasing order and appends only for shoppers, so output is ordered by shopper position.
Complexity
- Time: O(n), three linear passes.
- Space: O(n) for the two helper arrays, plus the output.
Reference solution
from typing import List
def closest_stalls(street: List[int]) -> List[int]:
n = len(street)
# prev_stall[i]: nearest stall index at or before i, or -1 if none
prev_stall = [-1] * n
last = -1
for i in range(n):
if street[i] == 2:
last = i
prev_stall[i] = last
# next_stall[i]: nearest stall index at or after i, or -1 if none
next_stall = [-1] * n
nxt = -1
for i in range(n - 1, -1, -1):
if street[i] == 2:
nxt = i
next_stall[i] = nxt
result = []
for i in range(n):
if street[i] == 1:
a, b = prev_stall[i], next_stall[i]
if a < 0:
result.append(b)
elif b < 0 or i - a <= b - i:
result.append(a)
else:
result.append(b)
return result