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
Dice Roll Simulation - DP Approach INPUT n = 2 rolls rollMax array: 1 i=1 1 i=2 2 i=3 2 i=4 2 i=5 3 i=6 Max consecutive rolls: Face 1: max 1 time Face 2: max 1 time Face 3-5: max 2 times Face 6: max 3 times [1,1] INVALID (1 can't repeat) [2,2] INVALID (2 can't repeat) ALGORITHM STEPS 1 Define DP State dp[i][j][k] = sequences at roll i, last face j, k consec 2 Base Case First roll: dp[1][j][1] = 1 for all faces j (1 to 6) 3 Transition Same face: k+1 if k < rollMax[j] Diff face: reset k to 1 4 Sum All States Answer = sum of dp[n][j][k] for all valid j and k DP Table (n=2): Face 1 2 3 4 5 6 Roll1 1 1 1 1 1 1 Roll2 5 5 6 6 6 6 5+5+6+6+6+6 = 34 (Face 1,2: no repeat allowed) FINAL RESULT Valid 2-Roll Sequences Face 1 first (5 ways): [1,2] [1,3] [1,4] [1,5] [1,6] Face 2 first (5 ways): [2,1] [2,3] [2,4] [2,5] [2,6] Face 3 first (6 ways): [3,1]..[3,6] including [3,3] Face 4 first (6 ways): [4,1]..[4,6] including [4,4] Face 5 first (6 ways): [5,1]..[5,6] including [5,5] Face 6 first (6 ways): [6,1]..[6,6] including [6,6] OUTPUT 34 Distinct sequences OK Key Insight: The DP state tracks three dimensions: current roll number, last face rolled, and consecutive count. When rolling the same face, we must check if consecutive count exceeds rollMax[face]. Time Complexity: O(n * 6 * max(rollMax)) | Space Complexity: O(n * 6 * max(rollMax)) TutorialsPoint - Dice Roll Simulation | Dynamic Programming Approach
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