The Number of the Smallest Unoccupied Chair - Problem

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.

For example, if chairs 0, 1, and 5 are occupied when a friend comes, they will sit on chair number 2.

When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.

You are given a 0-indexed 2D integer array times where times[i] = [arrivali, leavingi], indicating the arrival and leaving times of the ith friend respectively, and an integer targetFriend. All arrival times are distinct.

Return the chair number that the friend numbered targetFriend will sit on.

Input & Output

Example 1 — Basic Party Scenario
$ Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1
Output: 1
💡 Note: Friend 0 arrives at time 1, sits in chair 0. Friend 1 arrives at time 2, sits in chair 1 (smallest available). Friend 1 leaves at time 3, but we already found the answer.
Example 2 — Chair Reuse
$ Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0
Output: 2
💡 Note: Friend 1 arrives at time 1, sits in chair 0. Friend 2 arrives at time 2, sits in chair 1. Friend 0 arrives at time 3, sits in chair 2.
Example 3 — Immediate Chair Reuse
$ Input: times = [[1,2],[2,3],[3,4]], targetFriend = 2
Output: 0
💡 Note: Friend 0: arrives at 1, sits in chair 0, leaves at 2. Friend 1: arrives at 2, sits in chair 0 (just freed), leaves at 3. Friend 2: arrives at 3, sits in chair 0 (just freed again).

Constraints

  • n == times.length
  • 2 ≤ n ≤ 104
  • 1 ≤ arrivali < leavingi ≤ 105
  • 0 ≤ targetFriend ≤ n - 1
  • Each arrivali is distinct

Visualization

Tap to expand
Smallest Unoccupied Chair INPUT times array (arrival, leaving) Friend 0: 1 4 arrives@1, leaves@4 Friend 1 (TARGET): 2 3 arrives@2, leaves@3 Friend 2: 4 6 arrives@4, leaves@6 Timeline 1 2 3 4 5 6 F0 F1 F2 targetFriend = 1 ALGORITHM STEPS 1 Sort by Arrival Process friends in arrival order 2 Use Two Min-Heaps Available chairs + occupied chairs 3 Free Chairs Release chairs when friends leave 4 Assign Smallest Chair Pop from available heap Simulation: Time Event Available Chair t=1 F0 arr [0,1,2..] 0 t=2 F1 arr [1,2,3..] 1 t=3 F1 lv [1,2,3..] - t=4 F0,F2 [0,1,2..] 0 Available: 0 1 2 ... FINAL RESULT Chair Assignments at t=2 Chair 0 F0 Chair 1 F1 Chair 2 empty Chair 3 empty TARGET When Friend 1 arrives at time 2: Chair 0 is occupied by Friend 0 Chair 1 is the smallest available OUTPUT 1 OK - Chair 1 assigned! Friend 1 sits on chair 1 until time 3 Key Insight: Use TWO min-heaps: one for available chairs (sorted by chair number) and one for occupied chairs (sorted by leaving time). Process events by arrival time, freeing chairs before assigning. Time Complexity: O(n log n) | Space Complexity: O(n) TutorialsPoint - The Number of the Smallest Unoccupied Chair | Optimal Solution
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
28.5K Views
Medium Frequency
~25 min Avg. Time
893 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen