|
| 1 | +# 1942. The Number of the Smallest Unoccupied Chair |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Heap (Priority Queue), Ordered Set. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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**. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- For example, if chairs `0`, `1`, and `5` are occupied when a friend comes, they will sit on chair number `2`. |
| 14 | + |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +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**. |
| 19 | + |
| 20 | +Return** the **chair number** that the friend numbered **`targetFriend`** will sit on**. |
| 21 | + |
| 22 | + |
| 23 | +Example 1: |
| 24 | + |
| 25 | +``` |
| 26 | +Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1 |
| 27 | +Output: 1 |
| 28 | +Explanation: |
| 29 | +- Friend 0 arrives at time 1 and sits on chair 0. |
| 30 | +- Friend 1 arrives at time 2 and sits on chair 1. |
| 31 | +- Friend 1 leaves at time 3 and chair 1 becomes empty. |
| 32 | +- Friend 0 leaves at time 4 and chair 0 becomes empty. |
| 33 | +- Friend 2 arrives at time 4 and sits on chair 0. |
| 34 | +Since friend 1 sat on chair 1, we return 1. |
| 35 | +``` |
| 36 | + |
| 37 | +Example 2: |
| 38 | + |
| 39 | +``` |
| 40 | +Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0 |
| 41 | +Output: 2 |
| 42 | +Explanation: |
| 43 | +- Friend 1 arrives at time 1 and sits on chair 0. |
| 44 | +- Friend 2 arrives at time 2 and sits on chair 1. |
| 45 | +- Friend 0 arrives at time 3 and sits on chair 2. |
| 46 | +- Friend 1 leaves at time 5 and chair 0 becomes empty. |
| 47 | +- Friend 2 leaves at time 6 and chair 1 becomes empty. |
| 48 | +- Friend 0 leaves at time 10 and chair 2 becomes empty. |
| 49 | +Since friend 0 sat on chair 2, we return 2. |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +**Constraints:** |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +- `n == times.length` |
| 58 | + |
| 59 | +- `2 <= n <= 104` |
| 60 | + |
| 61 | +- `times[i].length == 2` |
| 62 | + |
| 63 | +- `1 <= arrivali < leavingi <= 105` |
| 64 | + |
| 65 | +- `0 <= targetFriend <= n - 1` |
| 66 | + |
| 67 | +- Each `arrivali` time is **distinct**. |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +## Solution |
| 72 | + |
| 73 | +```javascript |
| 74 | + |
| 75 | +``` |
| 76 | + |
| 77 | +**Explain:** |
| 78 | + |
| 79 | +nope. |
| 80 | + |
| 81 | +**Complexity:** |
| 82 | + |
| 83 | +* Time complexity : O(n). |
| 84 | +* Space complexity : O(n). |
0 commit comments