Ctrl K

The Lowest Free Stool

Medium
A bar counter has an endless row of stools numbered 0, 1, 2, … A total of n guests, numbered 0 to n - 1, come in. Every guest who walks in takes the free stool with the smallest number. You get an array visits where visits[i] = [arrive, leave] gives the arrival and departure times of guest i. A guest's stool becomes free exactly at time leave; anyone arriving at that same moment can already take it. All arrival times are distinct. Return the number of the stool that guest guest sits on.
Example 1
visits=[[3, 8], [1, 4], [6, 9], [4, 7]], guest=3 → 0

At time 1 guest 1 takes stool 0, at time 3 guest 0 takes stool 1. At time 4 guest 1 leaves, and guest 3, arriving at that very moment, takes the freed stool 0.

Example 2
visits=[[2, 5], [1, 10], [3, 6]], guest=2 → 2

Guests 1, 0 and 2 arrive at times 1, 2 and 3, and nobody has left by the time guest 2 comes: stools 0 and 1 are taken, so they get stool 2.

Constraints
  • n == len(visits)
  • 2 ≤ n ≤ 5·10⁴
  • len(visits[i]) == 2
  • 1 ≤ arrive < leave ≤ 10⁵
  • All arrival times are distinct
  • 0 ≤ guest ≤ n - 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Common Values MediumJobs With a Cooldown MediumSorted Into Runs of Three MediumMost Searched Words MediumSpread Out the Labels Medium
loading Python…
Input
visits=[[3, 8], [1, 4], [6, 9], [4, 7]], guest=3
Expected
0

Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.