Search⌘ K
AI Features

Solution: The Number of the Smallest Unoccupied Chair

Explore how to determine the smallest available chair number at a party using two min heaps. This lesson teaches you to handle dynamic chair assignment by processing friend arrivals and departures efficiently. You will learn to implement priority queues to track free and occupied chairs, optimize the solution with sorting, and return the chair number for a target friend with O(n log n) time complexity and linear space usage.

Statement

At a party, nn friends, numbered from 00 to n1n - 1, arrive and leave at different times. There are infinitely many chairs, numbered 00 onwards. Each arriving friend sits on the smallest available chair at that moment.

For example, ...