15% off all plans & roadmaps Use code
See plansLeetCode 1942 · Solution & editorial
There is a party where n friends numbered from 0 to n - 1 are attending. There is an infinite number of chairs in this party that are numbered from 0 to infinity. When a friend arrives at the party, they sit on the unoccupied chair with the smallest number.
The Number of the Smallest Unoccupied Chair is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. The pattern covers top K elements, K-way merge, two heaps for median, and scheduling
Solve scheduling and optimization problems using priority queue with greedy approach.
The reference solution runs in O(n log n) time and O(n) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
The Number of the Smallest Unoccupied Chair is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.