Seat Assignment with Forward-Only Probing on a Fixed Row
Data Structure DesignAuto-graded · 21 test casesLast updated
Decide whether every request in a sequence can be seated in a fixed row when taken spots push it to the next free higher slot; tests efficient next-free-slot lookup, e.g. with union-find.
A storage hall has n_slots slots in a single row, numbered 0 to n_slots - 1. Items are delivered one at a time, in the order given by the list wishes. The value wishes[i] is the slot number that the i-th item would like to occupy.
Your task is to decide whether all items can be stored.
Placement rules
- All slots are empty at the start. An item that has been stored stays in its slot permanently.
- An item is placed in its wished slot if that slot is empty.
- Otherwise, it is placed in the empty slot with the smallest number that is greater than its wished slot.
- Items never move to a slot with a number lower than the one they wished for. If every slot from the wished one to the last is occupied, that item cannot be stored.
Output
Return True if every item gets stored. Return False if at least one item cannot be stored. If wishes is empty, return True.
A solution that walks forward one slot at a time for each item can be too slow when many items crowd the same region, so aim for something better.
Constraints
1 <= n_slots <= 10^50 <= len(wishes) <= 10^50 <= wishes[i] <= n_slots - 1
Examples
Example 1
Input: all_items_stored(5, [2, 2, 0, 4])
Output: True
Example 2
Input: all_items_stored(4, [2, 2, 3])
Output: False
Example 3
Input: all_items_stored(6, [3, 0])
Output: True
Hints
Hint 1
Slots only ever go from empty to occupied, never back. Think about what question you need to answer quickly for each item: 'which is the smallest empty slot at or after position x?'
Hint 2
A disjoint-set (union-find) structure fits this well: let each occupied slot point to the next slot, so that find(x) returns the smallest empty slot at or after x.
Hint 3
Add a sentinel index equal to n_slots meaning 'no slot available'. After filling slot s, link it to find(s + 1). If find(wish) ever returns the sentinel, return False. Use path compression to keep lookups near constant time.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Simulating each item by scanning forward from its wished slot is correct but can cost O(n_slots) per item when a long block of slots is already full, giving O(n_slots * len(wishes)) in the worst case.
The key observation is that a slot, once occupied, stays occupied forever. So the question for each item is: "what is the smallest empty slot at or after position x?" Occupied slots can be permanently skipped, which is exactly what a disjoint-set (union-find) structure supports.
- Keep a
parentarray of sizen_slots + 1. Initiallyparent[x] = x, meaning every slot is empty and is its own answer. - Index
n_slotsis a sentinel meaning "no slot available". - For each wish, compute
slot = find(wish), the smallest empty slot at or after the wish.- If
slot == n_slots, nothing is available and the answer isFalse. - Otherwise, occupy it by setting
parent[slot] = find(slot + 1), so later lookups skip it.
- If
- If all wishes are processed, return
True.
Why it is correct
By induction, find(x) always returns the smallest empty slot with index at least x. Initially this holds trivially. When slot s is filled, we redirect it to the smallest empty slot after s, so any chain that reached s now continues to the next empty slot. Lookups only move forward, so no item is ever placed at a lower index than its wish, matching the rules. An empty wishes list never enters the loop and returns True.
Complexity
- Time: O((n_slots + len(wishes)) * alpha(n_slots)) with path compression, which is effectively linear.
- Space: O(n_slots) for the
parentarray.
Reference solution
from typing import List
def all_items_stored(n_slots: int, wishes: List[int]) -> bool:
# parent[x] leads to the smallest empty slot >= x; n_slots is a sentinel.
parent = list(range(n_slots + 1))
def find(x: int) -> int:
root = x
while parent[root] != root:
root = parent[root]
while parent[x] != x:
nxt = parent[x]
parent[x] = root
x = nxt
return root
for wish in wishes:
slot = find(wish)
if slot == n_slots:
return False
parent[slot] = find(slot + 1)
return True