A die simulator generates a random number from 1 to 6 for each roll. You introduced a constraint to the generator such that it cannot roll the number i more than rollMax[i] (1-indexed) consecutive times.
Given an array of integers rollMax and an integer n, return the number of distinct sequences that can be obtained with exact n rolls.
Two sequences are considered different if at least one element differs from each other. Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
1 2 3
Input: n = 2, rollMax = [1,1,2,2,2,3] Output: 34 Explanation: There will be 2 rolls of die, if there are no constraints on the die, there are 6 * 6 = 36 possible combinations. In this case, looking at rollMax array, the numbers 1 and 2 appear at most once consecutively, therefore sequences (1,1) and (2,2) cannot occur, so the final answer is 36-2 = 34.
classSolution { public: int dp[5000][6][16]; intdieSimulator(int n, vector<int>& rollMax){ int ans = 0; memset(dp, 0, sizeof(dp)); for (int i = 0; i < 6; ++i) { ans = (ans + dfs(rollMax, n-1, i, 1))%1000000007; } return ans; } intdfs(vector<int> &rollMax, int remaind, int last, int times){ if (remaind == 0) return1; if (dp[remaind][last][times] != 0) return dp[remaind][last][times]; int ans = 0; for (int n: {0,1,2,3,4,5}) { if (last == n) { if (times < rollMax[n]) { ans = (ans + dfs(rollMax, remaind-1, n, times+1))%1000000007; } } else { ans = (ans + dfs(rollMax, remaind-1, n, 1))%1000000007; } } return dp[remaind][last][times] = ans; } };