The Number of the Smallest Unoccupied Chair - Problem

Imagine hosting a party where n friends (numbered 0 to n-1) arrive and leave at different times! The venue has an infinite number of chairs numbered from 0 to infinity, and there's one simple rule: when a friend arrives, they must sit in the lowest-numbered available chair.

Here's how it works:

  • If chairs 0, 1, and 5 are occupied when someone arrives, they'll sit in chair 2
  • When a friend leaves, their chair becomes available immediately
  • If someone leaves and another person arrives at the exact same moment, the arriving person can take that chair

You're given a 2D array times where times[i] = [arrival_i, leaving_i] represents when friend i arrives and leaves. Your mission: find which chair number the targetFriend will sit on!

Note: All arrival times are distinct, so no two friends arrive simultaneously.

Input & Output

example_1.py โ€” Basic Party Scenario
$ Input: times = [[1,4],[2,3],[5,5]], targetFriend = 1
โ€บ Output: 1
๐Ÿ’ก Note: Friend 0 arrives at time 1 and sits in chair 0. Friend 1 arrives at time 2 and sits in chair 1. Friend 1 leaves at time 3. Friend 2 arrives at time 5 and sits in chair 0 (now available). Since we want targetFriend = 1, the answer is chair 1.
example_2.py โ€” Quick Turnaround
$ Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0
โ€บ Output: 2
๐Ÿ’ก Note: Friend 1 arrives at time 1 โ†’ chair 0. Friend 2 arrives at time 2 โ†’ chair 1. Friend 0 arrives at time 3 โ†’ chair 2. Since targetFriend = 0, the answer is chair 2.
example_3.py โ€” Single Friend
$ Input: times = [[1,2]], targetFriend = 0
โ€บ Output: 0
๐Ÿ’ก Note: Only one friend (friend 0) arrives at the party and gets the first available chair, which is chair 0.

Visualization

Tap to expand
๐ŸŽญ Party Chair Management System๐ŸŽ‰ Party Venue0123๐ŸŸข Available ๐Ÿ”ต Occupied โญ Target ChairF0F1F2Waiting...Available Chairs HeapMin-Heap: [0, 1, 2, 3, ...]โ€ข Always gives smallest chairโ€ข O(log n) insertion/removalโ€ข Automatically maintains orderOccupied Chairs HeapMin-Heap: [(time, chair), ...]โ€ข Sorted by departure timeโ€ข Process departures firstโ€ข Free chairs back to availableTime: O(n log n) | Space: O(n)Sorting + Heap Operations = Optimal Solution
Understanding the Visualization
1
Initial Setup
All chairs 0, 1, 2... are available. Sort friends by arrival time.
2
First Arrival
Friend arrives at time T, gets chair 0 (smallest available). Add (departure_time, 0) to occupied heap.
3
Subsequent Arrivals
Check if anyone left by now. If yes, free their chairs. Assign smallest available chair.
4
Chair Recycling
When friends leave, their chairs become available for future arrivals.
5
Target Found
When target friend arrives, return their assigned chair number.
Key Takeaway
๐ŸŽฏ Key Insight: Use two min-heaps to efficiently track available chairs and departure events, ensuring O(log n) operations for chair assignment and departure processing.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(n log n) for sorting + O(n log n) for heap operations (each friend does at most 2 heap operations)

n
2n
โšก Linearithmic
Space Complexity
O(n)

O(n) space for heaps storing chairs and departure events

n
2n
โšก Linearithmic Space

Constraints

  • n == times.length
  • 2 โ‰ค n โ‰ค 104
  • times[i].length == 2
  • 1 โ‰ค arrivali < leavingi โ‰ค 105
  • 0 โ‰ค targetFriend โ‰ค n - 1
  • All arrival times are distinct
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 15
68.2K Views
Medium-High Frequency
~18 min Avg. Time
1.8K 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