[LeetCode] First Day Where You Have Been in All the Rooms

1997. 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 109 + 7.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int firstDayBeenInAllRooms(vector<int>& nv) {
int mod(1e9 + 7);
vector<long> dp(nv.size() + 1);
for(int i = 1; i < nv.size(); i++) {
dp[i] = (2 * (dp[i - 1] + 1) - dp[nv[i - 1]] + mod) % mod;
}
return dp[nv.size() - 1] % mod;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/01/PS/LeetCode/first-day-where-you-have-been-in-all-the-rooms/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.