C++ Dp Using prefix sum
class Solution {\npublic:\n    int cnt[100001];\n    int ret[100001];\n    long long prefix[100001];\n    int M=1e9+7;\n    int firstDayBeenInAllRooms(vector& n
class Solution {\npublic:\n    int cnt[100001];\n    int ret[100001];\n    long long prefix[100001];\n    int M=1e9+7;\n    int firstDayBeenInAllRooms(vector<int>& nextVisit) {\n        int n=nextVisit.size();\n        memset(ret,-1,sizeof(ret));\n        memset(cnt,0,sizeof(cnt));\n        ret[0]=1;\n        cnt[0]=0;\n        prefix[0]=1;\n        if(n==0)\n        return 0;\n        long long time=2;\n        for(int i=1;i<n-1;i++)\n        {\n            cnt[i]++;\n            cnt[i]=cnt[i]%2;\n            if(cnt[i]==0)\n            {\n                time=(time+1)%M;\n                continue;\n            }\n            int x=nextVisit[i];\n                if(x==0)\n                {\n                  ret[i]=prefix[i-1]+i+1;\n                  prefix[i]=(prefix[i-1]+ret[i])%M;\n                }\n                else\n                {\n                    ret[i]=(prefix[i-1]-prefix[x-1]+i+1-x+M)%M;\n                    prefix[i]=(prefix[i-1]+ret[i])%M;\n                }\n                cnt[i]=0;\n                if(x>0)\n                time=(time+ret[i]+1)%M;\n                else\n                time=(time+ret[i]+1)%M;\n        }\n        return time;\n    }\n};
['Dynamic Programming', 'C', 'Prefix Sum']