Java | Top Down DP (Recursion + Memoization) | Clear & Clean Code
Code

# Code
```java []
class Solution {
    int modulo = 1000000007;

    public int dieSimulator(int n, int[] rollMax) {
        Integer[][][] dp = new Integer[n+1][6][16];
        return solve(n, rollMax, 0, 0, dp);
    }

    public int solve(int n, int[] rollMax, int last, int lastFreq, Integer[][][] dp){
        if(lastFreq > rollMax[last]) return 0;
        if(n == 0) return 1;
        if(dp[n][last][lastFreq] != null) return dp[n][last][lastFreq];

        int res = 0;
        for(int i = 0; i<6; i++){
            res += solve(n-1, rollMax, i, last == i ? lastFreq+1 : 1, dp);
            res %= modulo;
        }
        return dp[n][last][lastFreq] = res;
    }
}
```
['Java']