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 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
🎲 Dynamic Dice Sequence CounterState: (0,-1,0)Position: 0Last: NoneConsecutive: 0Try Roll: 1Limit: 1βœ“ ValidTry Roll: 2Limit: 1βœ“ ValidTry Roll: 6Limit: 3βœ“ ValidState: (1,1,1)Next roll 1?❌ Exceeds limit!State: (1,2,1)Next roll 2?❌ Exceeds limit!🧠 MemoizationState β†’ Count(0,-1,0) β†’ 34(1,1,1) β†’ 6(1,2,1) β†’ 6⚑ OptimizationBrute Force: O(6ⁿ)DP: O(nΓ—6Γ—maxRoll)Exponential β†’ Polynomial!🎯 Resultn=2, limits=[1,1,2,2,2,3]Valid sequences: 34Handles n=5000 efficiently!
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))!
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
18.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen