Search⌘ K
AI Features

Solution: The Number of the Smallest Unoccupied Chair

Understand how to use min heaps to manage chair assignments as friends arrive and leave, ensuring the smallest chair is always allocated efficiently. Explore sorting, heap operations, and a clear algorithm to solve the smallest unoccupied chair challenge, improving problem-solving skills 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, if chairs 00, 11, and 55 are occupied when a friend arrives, they will sit on chair number 22.

When a friend leaves, their chair becomes immediately available. If another friend arrives simultaneously, they can take that chair.

You are given a 00-indexed 2D2D list ...