Dice Roll Simulation - Problem
Dice Roll Simulation with Consecutive Limits
Imagine you're building a dice game simulator, but there's a twist! Your dice can't show the same number too many times in a row - just like how real dice games often have streak limits to keep things fair and exciting.
π² You have a standard 6-sided die (numbers 1-6) and need to simulate
Goal: Count how many distinct valid sequences of exactly
Input: An array
Output: Number of valid sequences modulo 109 + 7
Example: With
Imagine you're building a dice game simulator, but there's a twist! Your dice can't show the same number too many times in a row - just like how real dice games often have streak limits to keep things fair and exciting.
π² You have a standard 6-sided die (numbers 1-6) and need to simulate
n rolls. However, each number i can appear at most rollMax[i-1] consecutive times. For example, if rollMax[0] = 3, then the number 1 can appear at most 3 times in a row.Goal: Count how many distinct valid sequences of exactly
n dice rolls are possible.Input: An array
rollMax of 6 integers (limits for numbers 1-6) and integer n (number of rolls)Output: Number of valid sequences modulo 109 + 7
Example: With
rollMax = [1,1,2,2,2,3] and n = 2, sequences like [1,1] are invalid (1 appears twice but limit is 1), while [1,2] is valid. Input & Output
example_1.py β Basic Case
$
Input:
rollMax = [1,1,2,2,2,3], n = 2
βΊ
Output:
34
π‘ Note:
With 2 rolls and given limits: number 1 can appear max 1 consecutive time, number 2 can appear max 1 consecutive time, numbers 3,4,5 can appear max 2 consecutive times, and number 6 can appear max 3 consecutive times. Valid sequences include [1,2], [1,3], [1,4], [1,5], [1,6], [2,1], [2,3], etc. Total count is 34.
example_2.py β Longer Sequence
$
Input:
rollMax = [1,1,1,1,1,1], n = 3
βΊ
Output:
0
π‘ Note:
Each number can appear at most 1 consecutive time, but we need 3 rolls. Since no number can repeat consecutively and we only have 6 different numbers, it's impossible to create a valid sequence of length 3. Hence the answer is 0.
example_3.py β All Same Limit
$
Input:
rollMax = [2,2,2,2,2,2], n = 2
βΊ
Output:
36
π‘ Note:
Every number can appear up to 2 consecutive times. With 2 rolls, all possible combinations are valid: 6 choices for first roll Γ 6 choices for second roll = 36 total valid sequences.
Constraints
- 1 β€ n β€ 5000
- rollMax.length == 6
- 1 β€ rollMax[i] β€ 15
- Return answer modulo 109 + 7
Visualization
Tap to expand
Understanding the Visualization
1
State Tracking
Track current position, last rolled number, and how many times it appeared consecutively
2
Smart Transitions
For each state, calculate valid next moves by trying all 6 dice values
3
Constraint Checking
Only allow transitions that don't exceed consecutive roll limits
4
Memoization Magic
Cache results for each state to avoid recalculating the same subproblems
Key Takeaway
π― Key Insight: Transform exponential enumeration into polynomial counting by tracking (position, last_number, consecutive_count) states with memoization. This reduces O(6βΏ) to O(nΓ6Γmax(rollMax))!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code