101 Logo
onenoughtone

Problem Statement

Dice Roll Simulation

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] times consecutively.

Given an array of integers rollMax and an integer n, return the number of distinct sequences that can be obtained with exact 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.

Examples

Example 1:

Input: n = 2, rollMax = [1, 1, 2, 2, 2, 3]
Output: 34
Explanation: There are 34 possible sequences. For example, (1,1), (2,1), (3,1), (4,1), (5,1), (6,1), (1,2), (2,2), (3,2), (4,2), (5,2), (6,2), (1,3), (2,3), (3,3), (4,3), (5,3), (6,3), (1,4), (2,4), (3,4), (4,4), (5,4), (6,4), (1,5), (2,5), (3,5), (4,5), (5,5), (6,5), (1,6), (2,6), (3,6), (4,6).

Example 2:

Input: n = 2, rollMax = [1, 1, 1, 1, 1, 1]
Output: 30
Explanation: The only allowed sequences of length 2 are (1,2), (2,1), (2,3), (3,2), (3,4), (4,3), (4,5), (5,4), (5,6), (6,5), (1,3), (3,1), (1,4), (4,1), (1,5), (5,1), (1,6), (6,1), (2,4), (4,2), (2,5), (5,2), (2,6), (6,2), (3,5), (5,3), (3,6), (6,3), (4,6), (6,4).

Example 3:

Input: n = 3, rollMax = [1, 1, 1, 2, 2, 3]
Output: 181
Explanation: There are 181 possible sequences.

Constraints

  • 1 <= n <= 5000
  • rollMax.length == 6
  • 1 <= rollMax[i] <= 15

Problem Breakdown

To solve this problem, we need to:

  1. This problem can be solved using dynamic programming.
  2. We need to keep track of the number of sequences ending with each face value and the number of consecutive occurrences of that face.
  3. The state of the DP array is defined by the current roll number, the face value of the current roll, and the number of consecutive occurrences of that face.
  4. The recurrence relation involves considering all possible previous states that can lead to the current state.
  5. The modulo operation needs to be applied at each step to avoid integer overflow.
ProblemSolutionCode
101 Logo
onenoughtone