The Number of the Smallest Unoccupied Chair

Medium Topics Company Tags

Friends are numbered by their positions in times. Friend i arrives at times[i][0] and leaves at times[i][1]. All arrival times are distinct.

Chairs are numbered 0, 1, 2, .... Each arriving friend takes the smallest-numbered unoccupied chair. A chair becomes free at its occupant's leaving time, so a friend arriving at that same time can use it.

Return the chair assigned to the friend whose original index is targetFriend.

Example 1:

Input: times = [[1,3],[2,5],[3,6]]
targetFriend = 2

Output: 0

Explanation: Friend 0 takes chair 0 and friend 1 takes chair 1. At time 3, friend 0 leaves just as friend 2 arrives. Chair 0 is immediately available, so friend 2 takes it.

Example 2:

Input: times = [[5,9],[1,8],[3,7]]
targetFriend = 0

Output: 2

Explanation: Friend 1 arrives first and takes chair 0. Friend 2 then takes chair 1. Both remain seated when friend 0 arrives at time 5, so the target takes chair 2.

Example 3:

Input: times = [[4,8],[1,2],[2,4]]
targetFriend = 0

Output: 0

Explanation: Friend 1 uses chair 0 until time 2. Friend 2 arrives then and reuses it until time 4. Friend 0 arrives at time 4 and can reuse chair 0 again.

Constraints:

  • 2 <= times.length <= 10000
  • Each entry of times contains exactly two integers.
  • 1 <= times[i][0] < times[i][1] <= 100000
  • All arrival times are distinct.
  • 0 <= targetFriend < times.length


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.