871. Limited Repeat Rolls

Hard7.5 points estimatedArrayDynamic Programming3 hints

A board-game die has faces numbered 1 to 6. It is rolled n times and the sequence of faces is recorded. A rule forbids rolling face j+1 more than rollMax[j] times in a row (consecutively), so for example with rollMax[0] = 2 the face 1 may appear at most twice in a row.

Return how many different recorded sequences of length n obey the rule for every face. Two sequences are different if they differ at some position. Because the count can be huge, return it modulo 1000000007. rollMax always has exactly 6 entries.

Example 1

Input:
n = 2, rollMax = [1,1,2,2,2,3]
Output:
34
Explanation:

Two rolls, face 1 and face 2 may not repeat, face 6 can repeat up to 3 times: all 36 pairs are fine except (1,1) and (2,2), giving 34.

Example 2

Input:
n = 3, rollMax = [1,1,1,1,1,1]
Output:
150
Explanation:

No face may follow itself, so each roll differs from the previous one: 6 * 5 * 5 = 150 sequences.

Example 3

Input:
n = 1, rollMax = [1,1,1,1,1,1]
Output:
6
Explanation:

A single roll can show any of the 6 faces, so there are 6 sequences.

Constraints

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

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(36 * n)
Space
O(6 * n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…