C++ || Recursion + Memoization || DP with explanation
Here I am iterating i from 1 to 6 (i.e the value that can occur in dice on rolling) and checking if the previous element is equal to i and its freq is greater t
Here I am iterating i from 1 to 6 (i.e the value that can occur in dice on rolling) and checking if the previous element is equal to i and its freq is greater than zero then it can be used in the sequence , else freq from the rollMax table is being passed to make a different sequence.\n```\nclass Solution {\npublic:\n    int recursion(int n , int prev, int prev_freq ,vector<int>&roll , vector<vector<vector<int>>>&memo,int &mod){\n        if(n == 0)return 1; //base case \n        if(memo[n][prev][prev_freq] != -1)return memo[n][prev][prev_freq];\n        int count = 0;\n        for(int i = 1 ;i <= 6;i++){\n            if(i == prev and prev_freq >0){\n                count = (count + recursion(n -1 , i , prev_freq -1, roll, memo, mod))%mod;\n            }\n            else if(i != prev){\n                count = (count + recursion(n-1 , i, roll[i-1] -1, roll, memo, mod))%mod;\n            }\n        }\n        return memo[n][prev][prev_freq] = count %mod;\n    }\n    int dieSimulator(int n, vector<int>& rollMax) {\n        vector<vector<vector<int>>>memo(n + 1,vector<vector<int>>(7, vector<int>(16,-1))); // no of elements are 6  and max value in rollMax is 15\n        int mod = 1000000007;\n        return recursion(n, 0, 0, rollMax, memo, mod) % mod;\n    }\n};
['Dynamic Programming', 'Memoization', 'C']