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
Dice Roll Simulation: Count Valid Sequences with ConstraintsInputn = 2[1,1,2,2,2,3]Constraint CheckMax consecutive: 1→1, 2→1, 3→2, etc.Valid: [1,2], [2,3], Invalid: [1,1], [2,2]Output34sequencesValid Sequences Examples[1,2] ✓ [1,3] ✓ [1,4] ✓ [1,5] ✓ [1,6] ✓[2,1] ✓ [2,3] ✓ [2,4] ✓ [2,5] ✓ [2,6] ✓[1,1] ✗ [2,2] ✗ (exceed consecutive limits)... and more valid combinations🎯 Key Insight: Use DP to track (position, last_number, consecutive_count) states
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
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
28.5K Views
Medium Frequency
~35 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