#1997
First Day Where You Have Been in All the Rooms
expert · 1020 · lc medium +32 · verified · 40.5% accepted · 510 likes · top 20%
Description
There are n rooms labeled 0 to n - 1. Starting on day 0 in room 0, you visit one room per day according to these rules (using a 0-indexed array nextVisit):
- When you visit room i for an odd number of times, the next day go to room nextVisit[i] (where 0 <= nextVisit[i] <= i).
- When you visit room i for an even number of times, the next day go to room (i + 1) mod n.
Return the day number on which you first visit every room at least once, modulo 109 + 7. A solution is guaranteed to exist.
Example 1:
Input: nextVisit = [0,0]
Output: 2
Explanation:
- On day 0, you visit room 0. The total times you have been in room 0 is 1, which is odd.
On the next day you will visit room nextVisit[0] = 0
- On day 1, you visit room 0, The total times you have been in room 0 is 2, which is even.
On the next day you will visit room (0 + 1) mod 2 = 1
- On day 2, you visit room 1. This is the first day where you have been in all the rooms.
Example 2:
Input: nextVisit = [0,0,2]
Output: 6
Explanation:
Your room visiting order for each day is: [0,0,1,0,0,1,2,...].
Day 6 is the first day where you have been in all the rooms.
Example 3:
Input: nextVisit = [0,1,2,0]
Output: 6
Explanation:
Your room visiting order for each day is: [0,0,1,1,2,2,3,...].
Day 6 is the first day where you have been in all the rooms.
Code
1
2
3