1223. Dice Roll Simulation
SourceWeekly Contest 158 Q3DifficultyHardRating2008
Description
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. Since the answer may be too large, return it modulo 109 + 7.
Two sequences are considered different if at least one element differs from each other.
Example 1:
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.
Example 2:
Input: n = 2, rollMax = [1,1,1,1,1,1] Output: 30
Example 3:
Input: n = 3, rollMax = [1,1,1,2,2,3] Output: 181
Constraints:
1 <= n <= 5000rollMax.length == 61 <= rollMax[i] <= 15
Solutions
Solution 1: Memoization Search
Thinking
A face may repeat only \(rollMax\) times in a row. \(n\) reaches \(5000\), so listing sequences is impossible. The count depends only on remaining rolls, the last face, and its current streak.
Memoized \(dfs(i,j,x)\) starts at roll \(i\) after face \(j\) with streak \(x\). The next face \(k\) resets the streak if \(k\ne j\), or may continue if the cap allows. The start state \((0,0,0)\) means no roll yet.
There are \(O(n\times 6\times 15)\) states and \(6\) transitions; memoization shares subproblems.
We can design a function \(dfs(i, j, x)\) to represent the number of schemes starting from the \(i\)-th dice roll, with the current dice roll being \(j\), and the number of consecutive times \(j\) is rolled being \(x\). The range of \(j\) is \([1, 6]\), and the range of \(x\) is \([1, rollMax[j - 1]]\). The answer is \(dfs(0, 0, 0)\).
The calculation process of the function \(dfs(i, j, x)\) is as follows:
- If \(i \ge n\), it means that \(n\) dice have been rolled, return \(1\).
- Otherwise, we enumerate the number \(k\) rolled next time. If \(k \ne j\), we can directly roll \(k\), and the number of consecutive times \(j\) is rolled will be reset to \(1\), so the number of schemes is \(dfs(i + 1, k, 1)\). If \(k = j\), we need to judge whether \(x\) is less than \(rollMax[j - 1]\). If it is less, we can continue to roll \(j\), and the number of consecutive times \(j\) is rolled will increase by \(1\), so the number of schemes is \(dfs(i + 1, j, x + 1)\). Finally, add all the scheme numbers to get the value of \(dfs(i, j, x)\). Note that the answer may be very large, so we need to take the modulus of \(10^9 + 7\).
During the process, we can use memoization search to avoid repeated calculations.
The time complexity is \(O(n \times k^2 \times M)\), and the space complexity is \(O(n \times k \times M)\). Here, \(k\) is the range of dice points, and \(M\) is the maximum number of times a certain point can be rolled consecutively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Solution 2: Dynamic Programming
Thinking
Memoization follows the same transition in recursive order. Bottom-up, \(f[i][j][x]\) is the count after \(i\) rolls ending with face \(j\) and streak \(x\), accumulated from layer \(i-1\) by the same rules. This removes the call stack and invites a rolling array.
We can change the memoization search in Solution 1 to dynamic programming.
Define \(f[i][j][x]\) as the number of schemes for the first \(i\) dice rolls, with the \(i\)-th dice roll being \(j\), and the number of consecutive times \(j\) is rolled being \(x\). Initially, \(f[1][j][1] = 1\), where \(1 \leq j \leq 6\). The answer is:
We enumerate the last dice roll as \(j\), and the number of consecutive times \(j\) is rolled as \(x\). The current dice roll can be \(1, 2, \cdots, 6\). If the current dice roll is \(k\), there are two cases:
- If \(k \neq j\), we can directly roll \(k\), and the number of consecutive times \(j\) is rolled will be reset to \(1\). Therefore, the number of schemes \(f[i][k][1]\) will increase by \(f[i-1][j][x]\).
- If \(k = j\), we need to judge whether \(x+1\) is less than or equal to \(rollMax[j-1]\). If it is less than or equal to, we can continue to roll \(j\), and the number of consecutive times \(j\) is rolled will increase by \(1\). Therefore, the number of schemes \(f[i][j][x+1]\) will increase by \(f[i-1][j][x]\).
The final answer is the sum of all \(f[n][j][x]\).
The time complexity is \(O(n \times k^2 \times M)\), and the space complexity is \(O(n \times k \times M)\). Here, \(k\) is the range of dice points, and \(M\) is the maximum number of times a certain point can be rolled consecutively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |