Search⌘ K
AI Features

Solution: The Number of the Smallest Unoccupied Chair

Explore how to use min heaps to efficiently solve the problem of assigning the smallest unoccupied chair to arriving friends at a party. This lesson helps you understand managing dynamic availability with heaps, sorting by arrival times, and optimizing chair assignment with time complexity O(n log n). Learn to apply heap-based solutions for scheduling and resource allocation problems common in coding interviews.

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