C++ || 3D DP || Memoization || Explained with comments
\nclass Solution {\npublic:\n    int mod = 1e9+7;\n    int dfs(int n,vector<int>& rollMax,int prevRoll,int freq,vector<vector<vector<int>>>& dp){\n        if(n=
```\nclass Solution {\npublic:\n    int mod = 1e9+7;\n    int dfs(int n,vector<int>& rollMax,int prevRoll,int freq,vector<vector<vector<int>>>& dp){\n        if(n==0) return 1; //if n becomes 0 that means we successfully created one permutation of dice roll\n        if(dp[n][prevRoll][freq]!=-1) return dp[n][prevRoll][freq];\n        int ans = 0;\n        for(int i = 1;i<=6;i++){\n            if(i==prevRoll){ //if current roll value is same as prevRoll value\n                if(freq<rollMax[prevRoll-1]){ //we are checking freq of prevRoll value occured doesn\'t exceed rollMax value specified for it\n                    ans = (ans+dfs(n-1,rollMax,prevRoll,freq+1,dp))%mod;\n                }\n            }\n            else{\n                ans = (ans+dfs(n-1,rollMax,i,1,dp))%mod;\n            }\n        }\n        return dp[n][prevRoll][freq] = ans;\n    }\n    \n    int dieSimulator(int n, vector<int>& rollMax) {\n        //dp states are :\n        //1. n\n        //2. prev roll value\n        //3. how many consequetive time prev roll has occured\n        vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(7,vector<int>(16,-1)));\n        int ans = dfs(n,rollMax,0,0,dp);\n        return ans;\n    }\n};\n```\nYou can **upvote** , if this post is useful\nThankyou!!\n\n**Another similar practice problem : **\nPaint House 3 : https://leetcode.com/problems/paint-house-iii/discuss/2255185/c-memoization-3d-dp-explained-with-comments
['Dynamic Programming', 'C']