First Day Where You Have Been in All the Rooms LeetCode solution in Java
1class Solution {2public int firstDayBeenInAllRooms(int[] nextVisit) {3int rooms = nextVisit.length;4long dp[] = new long[rooms];5int mod = (int) (1e9) + 7;6for (int i = 1; i < rooms; i++) {7if (nextVisit[i - 1] == i - 1) dp[i] = dp[i - 1] + 2;8else dp[i] = (dp[i - 1] + dp[i - 1] - dp[nextVisit[i - 1]] + 2 + mod) % mod;9}10return (int) dp[rooms - 1];11}12}