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:
150
💡 Note:
With 3 rolls and no consecutive repeats allowed, we have 6 choices for first roll, 5 choices for second roll (can't repeat first), and 5 choices for third roll (can't repeat second). Total: 6 × 5 × 5 = 150 valid sequences.
Constraints
- 1 ≤ n ≤ 5000
- rollMax.length == 6
- 1 ≤ rollMax[i] ≤ 15
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code