Java DP intiutive
Let\'s first build up an intiution using recusion than modify with dynamic programming.\n\nAs the problem demands to restrict repetion of same value generation 
Let\'s first build up an intiution using recusion than modify with dynamic programming.\n\nAs the problem demands to restrict repetion of same value generation by generator we can simply do a recusion and go through all the possible permutation in brute force. \n\nIn a recusion call, for a each number[1,6] we can repeately generate same value until it reaches its limit. \nAlso we have to take into consideration we may not select same in number in succsive recusion call.\n\n\n```\n    public int recur(int n, int pre){\n        if(n==0){\n            return 1;\n        }\n        int ans=0;\n\t\t// loop for each valid dice value\n        for(int i=0;i<6;i++){\n\t\t\t// do not generate for number generated in previous recusion call (parent\'s number)\n            if(i==pre){\n                continue;\n            }\n            // loop until with number limit or total limit is reached\n            for(int fre=1;fre<=n && fre<=rollMax[i] ; fre++){\n\t\t\t\t// now the current selected value i is previous(pre) to child call\n                ans+= recur(n-fre, i);\n                if(ans>=mod){\n                    ans-=mod;\n                }\n            }\n        }\n        return ans;\n    }\n```\n\n\n## Improving performance using DP.\n#### Note that first number to be selected for a permutation could be any value so we pass -1 initially.\n```\n\nclass Solution {\n    \n    int[] rollMax;\n    int mod= (int)1e9+ 7;\n    int[][] dp;\n    \n    public int recur(int n, int pre){\n        if(n==0){\n            return 1;\n        }\n        if(pre!=-1 && dp[n][pre]!=-1){\n            return dp[n][pre];\n        }\n        int ans=0;\n        for(int i=0;i<6;i++){\n            if(i==pre){\n                continue;\n            }\n            \n            for(int fre=1;fre<=n && fre<=rollMax[i] ; fre++){\n                ans+= recur(n-fre, i);\n                if(ans>=mod){\n                    ans-=mod;\n                }\n            }\n        }\n        if(pre!=-1){\n            dp[n][pre]= ans;\n        }\n        return ans;\n    }\n    \n    public int dieSimulator(int n, int[] rollMax) {\n        this.rollMax = rollMax;\n        dp= new int[n+1][6];\n        for(int[] a:dp){\n            Arrays.fill(a, -1);\n        }\n        return recur(n, -1);\n    }\n    \n}\n```
['Dynamic Programming', 'Recursion']