1997. First Day Where You Have Been in All the Rooms Medium
1/**
2 * [1997] First Day Where You Have Been in All the Rooms
3 *
4 * There are n rooms you need to visit, labeled from 0 to n - 1. Each day is labeled, starting from 0. You will go in and visit one room a day.
5 * Initially on day 0, you visit room 0. The order you visit the rooms for the coming days is determined by the following rules and a given 0-indexed array nextVisit of length n:
6 *
7 * Assuming that on a day, you visit room i,
8 * if you have been in room i an odd number of times (including the current visit), on the next day you will visit a room with a lower or equal room number specified by nextVisit[i] where 0 <= nextVisit[i] <= i;
9 * if you have been in room i an even number of times (including the current visit), on the next day you will visit room (i + 1) mod n.
10 *
11 * Return the label of the first day where you have been in all the rooms. It can be shown that such a day exists. Since the answer may be very large, return it modulo 10^9 + 7.
12 *
13 * Example 1:
14 *
15 * Input: nextVisit = [0,0]
16 * Output: 2
17 * Explanation:
18 * - On day 0, you visit room 0. The total times you have been in room 0 is 1, which is odd.
19 * On the next day you will visit room nextVisit[0] = 0
20 * - On day 1, you visit room 0, The total times you have been in room 0 is 2, which is even.
21 * On the next day you will visit room (0 + 1) mod 2 = 1
22 * - On day 2, you visit room 1. This is the first day where you have been in all the rooms.
23 *
24 * Example 2:
25 *
26 * Input: nextVisit = [0,0,2]
27 * Output: 6
28 * Explanation:
29 * Your room visiting order for each day is: [0,0,1,0,0,1,2,...].
30 * Day 6 is the first day where you have been in all the rooms.
31 *
32 * Example 3:
33 *
34 * Input: nextVisit = [0,1,2,0]
35 * Output: 6
36 * Explanation:
37 * Your room visiting order for each day is: [0,0,1,1,2,2,3,...].
38 * Day 6 is the first day where you have been in all the rooms.
39 *
40 *
41 * Constraints:
42 *
43 * n == nextVisit.length
44 * 2 <= n <= 10^5
45 * 0 <= nextVisit[i] <= i
46 *
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/first-day-where-you-have-been-in-all-the-rooms/
51// discuss: https://leetcode.com/problems/first-day-where-you-have-been-in-all-the-rooms/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56 pub fn first_day_been_in_all_rooms(next_visit: Vec<i32>) -> i32 {
57 0
58 }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_1997() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.