Dynamic Programming| BEGINNER|C++
class Solution {\npublic:\n    int firstDayBeenInAllRooms(vector& arr) \n    {   \n    \n        int n=arr.size(),mod=1e9+7;\n        \n        if(n==2)\n      
class Solution {\npublic:\n    int firstDayBeenInAllRooms(vector<int>& arr) \n    {   \n    \n        int n=arr.size(),mod=1e9+7;\n        \n        if(n==2)\n            return 2;\n        \n        vector<int>dp(n-1);\n        dp[0]=0;\n        \n        int d=2; // present day \n        for(int i=1;i<n-1;i++)\n        {\n            dp[i]=d; // first time i entered this index on this day\n             \n            if(arr[i]!=i)  // present day-day i visted that index\n            {\n                d=(d+(d-dp[arr[i]]+mod)%mod)%mod;\n            }\n            \n            d=(d+2)%mod; // every index req.atleast 2 days to jump ahead\n        }\n        \n        return d;\n        \n    }\n};
['Dynamic Programming']