#1223
Dice Roll Simulation
candidate master · 1425 · lc hard +32 · verified · 50.7% accepted · 998 likes · top 39%
Description
A simulated die produces values from 1 to 6. A constraint prevents face i from appearing more than rollMax[i] times consecutively (1-indexed).
Given the constraint array rollMax and an integer n, count the number of distinct length-n sequences that satisfy the constraint. Two sequences differ if any position has a different value.
Return the count modulo 109 + 7.
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
Code
1
2
3