C++ Simple DP Approach
Intuition\nUpon looking at the question we have a feel that i th room visits depends upon i-1th room visit\'s and so on. Hence we approach the question through 
# Intuition\nUpon looking at the question we have a feel that i th room visits depends upon i-1th room visit\'s and so on. Hence we approach the question through dp.\n\n# Approach\n- The first line of thought to have a dp state is staraight forward. The vector **dp[i]** represent the first day the ith room is reached.\n- The transition is: **dp[i] = dp[i-1] + 2 + (dp[i-1] - dp[nextVisit[i-1]])** The transition can be seen by dry running throught the example code \n- Then the base case is decided which are dp[0] = 0 and dp[1] = 2\n- Then iterative dp is performed\n- To get the code under constrants modular arithmatic is used. Remember we are subtracting a term hence we are using +M and then taking the mod.\n\n# Complexity\n- Time complexity:\nAs the loop is running from 1 to N. And all other opperations are O(1) operations. The TC of the code is O(N)\n\n- Space complexity:\nAs we are storing n values in the dp at time. We can say that the space complexity is also o(N).\n\n# Code\n```\nclass Solution {\npublic:\n    int firstDayBeenInAllRooms(vector<int>& nextVisit) {\n        int n = nextVisit.size();\n        const int M = 1e9+7;\n        int sum =0;\n        vector<int> dp(n,0);\n        visits[0] =1;\n        dp[0] = 0;\n        dp[1] = 2;\n        for(int i=2;i<n;i++){\n            dp[i] = ((dp[i-1] + 2)%M +dp[i-1])%M;\n            dp[i] =  ((dp[i] - dp[nextVisit[i-1]])%M + M)%M;\n        }\n        return dp[n-1];\n    }\n};\n```
['C++']