Search⌘ K
AI Features

Solution: The Number of the Smallest Unoccupied Chair

Explore how to use min heaps to assign the smallest unoccupied chair to friends arriving and leaving at different times. This lesson teaches sorting by arrival, managing chair availability with two heaps, and returning the target friend's chair number efficiently, optimizing for O(n log n) time.

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, ...