DP explained with comments [JAVA]
\n\nclass Solution {\n    public int firstDayBeenInAllRooms(int[] nextVisit) {\n        int n = nextVisit.length;\n        long[] dp = new long[n];\n        int
\n```\nclass Solution {\n    public int firstDayBeenInAllRooms(int[] nextVisit) {\n        int n = nextVisit.length;\n        long[] dp = new long[n];\n        int mod = 1000000007;\n        dp[0] = 2;\n        \n        for(int i = 1; i < n; i++){\n            \n            if(i == n - 1){\n                break;\n            }\n            \n            //days needed to reach i from 0 index\n            long days = dp[i - 1];\n            \n            //days needed to reach i from nextVisit[i]\n            //prefix sum technique just subtracting dp[nextVisit[i] - 1]\n            if(nextVisit[i] - 1 >= 0){\n                days = (days - dp[nextVisit[i] - 1] + mod) % mod;\n            }\n            \n            //updating dp table\n            dp[i] = (dp[i - 1] + days + 2) % mod;\n        }\n        \n        return (int)dp[n - 2];\n    }\n}\n```
['Dynamic Programming', 'Java']