First Day Where You Have Been in All the Rooms

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.

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:

  • Assuming that on a day, you visit room i:
  • 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.
  • 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.

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.

Example 1
InputnextVisit = [0,0]
Output2
After visiting room 0 on days 0 and 1, day 2 is the first day room 1 is visited, so all rooms have been visited.
Example 2
InputnextVisit = [0,0,2]
Output6
The room visiting order begins [0, 0, 1, 0, 0, 1, 2, ...], so day 6 is the first day where all rooms have been visited.

Constraints

  • n == nextVisit.length
  • 2 <= n <= 10^5
  • 0 <= nextVisit[i] <= i

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate