|
| 1 | +/** |
| 2 | + * [1942] The Number of the Smallest Unoccupied Chair |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * |
| 6 | + * For example, if chairs 0, 1, and 5 are occupied when a friend comes, they will sit on chair number 2. |
| 7 | + * |
| 8 | + * 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. |
| 9 | + * You are given a 0-indexed 2D integer array times where times[i] = [arrivali, leavingi], indicating the arrival and leaving times of the i^th friend respectively, and an integer targetFriend. All arrival times are distinct. |
| 10 | + * Return the chair number that the friend numbered targetFriend will sit on. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * |
| 14 | + * Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1 |
| 15 | + * Output: 1 |
| 16 | + * Explanation: |
| 17 | + * - Friend 0 arrives at time 1 and sits on chair 0. |
| 18 | + * - Friend 1 arrives at time 2 and sits on chair 1. |
| 19 | + * - Friend 1 leaves at time 3 and chair 1 becomes empty. |
| 20 | + * - Friend 0 leaves at time 4 and chair 0 becomes empty. |
| 21 | + * - Friend 2 arrives at time 4 and sits on chair 0. |
| 22 | + * Since friend 1 sat on chair 1, we return 1. |
| 23 | + * |
| 24 | + * Example 2: |
| 25 | + * |
| 26 | + * Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0 |
| 27 | + * Output: 2 |
| 28 | + * Explanation: |
| 29 | + * - Friend 1 arrives at time 1 and sits on chair 0. |
| 30 | + * - Friend 2 arrives at time 2 and sits on chair 1. |
| 31 | + * - Friend 0 arrives at time 3 and sits on chair 2. |
| 32 | + * - Friend 1 leaves at time 5 and chair 0 becomes empty. |
| 33 | + * - Friend 2 leaves at time 6 and chair 1 becomes empty. |
| 34 | + * - Friend 0 leaves at time 10 and chair 2 becomes empty. |
| 35 | + * Since friend 0 sat on chair 2, we return 2. |
| 36 | + * |
| 37 | + * |
| 38 | + * Constraints: |
| 39 | + * |
| 40 | + * n == times.length |
| 41 | + * 2 <= n <= 10^4 |
| 42 | + * times[i].length == 2 |
| 43 | + * 1 <= arrivali < leavingi <= 10^5 |
| 44 | + * 0 <= targetFriend <= n - 1 |
| 45 | + * Each arrivali time is distinct. |
| 46 | + * |
| 47 | + */ |
| 48 | +pub struct Solution {} |
| 49 | + |
| 50 | +// problem: https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/ |
| 51 | +// discuss: https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/?currentPage=1&orderBy=most_votes&query= |
| 52 | + |
| 53 | +// submission codes start here |
| 54 | + |
| 55 | +impl Solution { |
| 56 | + pub fn smallest_chair(times: Vec<Vec<i32>>, target_friend: i32) -> i32 { |
| 57 | + 0 |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// submission codes end |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + #[ignore] |
| 69 | + fn test_1942_example_1() { |
| 70 | + let times = vec![vec![1, 4], vec![2, 3], vec![4, 6]]; |
| 71 | + let target_friend = 1; |
| 72 | + |
| 73 | + let result = 1; |
| 74 | + |
| 75 | + assert_eq!(Solution::smallest_chair(times, target_friend), result); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + #[ignore] |
| 80 | + fn test_1942_example_2() { |
| 81 | + let times = vec![vec![3, 10], vec![1, 5], vec![2, 6]]; |
| 82 | + let target_friend = 0; |
| 83 | + |
| 84 | + let result = 2; |
| 85 | + |
| 86 | + assert_eq!(Solution::smallest_chair(times, target_friend), result); |
| 87 | + } |
| 88 | +} |
0 commit comments