Dice Roll Simulation - Problem
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 exactly 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.
Input & Output
Example 1 — Basic Constraint
$
Input:
n = 2, rollMax = [1,1,2,2,2,3]
›
Output:
34
💡 Note:
With 2 rolls and max consecutive limits, we count all valid sequences. For example: [1,2], [1,3], [2,1], [2,3], etc. Invalid: [1,1] (exceeds rollMax[0]=1), [2,2,2] would exceed rollMax[1]=1.
Example 2 — Single Roll
$
Input:
n = 1, rollMax = [1,1,1,1,1,1]
›
Output:
6
💡 Note:
With only 1 roll, all numbers 1-6 are valid since no consecutive constraint is violated. Each number can be rolled once.
Example 3 — High Limits
$
Input:
n = 3, rollMax = [1,1,1,1,1,1]
›
Output:
181
💡 Note:
With 3 rolls and no consecutive repeats allowed, we must alternate numbers. Many valid sequences like [1,2,3], [1,2,4], [2,1,3], etc.
Constraints
- 1 ≤ n ≤ 5000
- rollMax.length == 6
- 1 ≤ rollMax[i] ≤ 15
Visualization
Tap to expand
Understanding the Visualization
1
Input
n=2 rolls, rollMax=[1,1,2,2,2,3] (max consecutive for each die face)
2
Process
Count all valid sequences respecting consecutive limits
3
Output
Total number of distinct valid sequences
Key Takeaway
🎯 Key Insight: Track the last rolled number and its consecutive count as state in dynamic programming
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code