Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 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.
Example 1:
Input: n = 2, rollMax = [1,1,2,2,2,3] Output: 34 Explanation: There will be 2 rolls of die, if there are no constraints on the die, there are 6 * 6 = 36 possible combinations. In this case, looking at rollMax array, the numbers 1 and 2 appear at most once consecutively, therefore sequences (1,1) and (2,2) cannot occur, so the final answer is 36-2 = 34.
Example 2:
Input: n = 2, rollMax = [1,1,1,1,1,1] Output: 30
Example 3:
Input: n = 3, rollMax = [1,1,1,2,2,3] Output: 181
Constraints:
1 <= n <= 5000rollMax.length == 61 <= rollMax[i] <= 15Problem summary: 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 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
2 [1,1,2,2,2,3]
2 [1,1,1,1,1,1]
3 [1,1,1,2,2,3]
find-missing-observations)number-of-distinct-roll-sequences)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1223: Dice Roll Simulation
class Solution {
private Integer[][][] f;
private int[] rollMax;
public int dieSimulator(int n, int[] rollMax) {
f = new Integer[n][7][16];
this.rollMax = rollMax;
return dfs(0, 0, 0);
}
private int dfs(int i, int j, int x) {
if (i >= f.length) {
return 1;
}
if (f[i][j][x] != null) {
return f[i][j][x];
}
long ans = 0;
for (int k = 1; k <= 6; ++k) {
if (k != j) {
ans += dfs(i + 1, k, 1);
} else if (x < rollMax[j - 1]) {
ans += dfs(i + 1, j, x + 1);
}
}
ans %= 1000000007;
return f[i][j][x] = (int) ans;
}
}
// Accepted solution for LeetCode #1223: Dice Roll Simulation
func dieSimulator(n int, rollMax []int) int {
f := make([][7][16]int, n)
const mod = 1e9 + 7
var dfs func(i, j, x int) int
dfs = func(i, j, x int) int {
if i >= n {
return 1
}
if f[i][j][x] != 0 {
return f[i][j][x]
}
ans := 0
for k := 1; k <= 6; k++ {
if k != j {
ans += dfs(i+1, k, 1)
} else if x < rollMax[j-1] {
ans += dfs(i+1, j, x+1)
}
}
f[i][j][x] = ans % mod
return f[i][j][x]
}
return dfs(0, 0, 0)
}
# Accepted solution for LeetCode #1223: Dice Roll Simulation
class Solution:
def dieSimulator(self, n: int, rollMax: List[int]) -> int:
@cache
def dfs(i, j, x):
if i >= n:
return 1
ans = 0
for k in range(1, 7):
if k != j:
ans += dfs(i + 1, k, 1)
elif x < rollMax[j - 1]:
ans += dfs(i + 1, j, x + 1)
return ans % (10**9 + 7)
return dfs(0, 0, 0)
// Accepted solution for LeetCode #1223: Dice Roll Simulation
struct Solution;
const M: i32 = 1_000_000_007;
impl Solution {
fn die_simulator(n: i32, roll_max: Vec<i32>) -> i32 {
let n = n as usize;
let mut dp: Vec<Vec<Vec<i32>>> = vec![vec![vec![0; 5001]; 16]; 6];
Self::dfs(n, -1, 1, &mut dp, &roll_max)
}
fn dfs(
n: usize,
prev: i32,
repeat: usize,
dp: &mut Vec<Vec<Vec<i32>>>,
roll_max: &[i32],
) -> i32 {
if n == 0 {
1
} else {
if prev >= 0 && dp[prev as usize][repeat][n] > 0 {
return dp[prev as usize][repeat][n];
}
let mut sum = 0;
for i in 0..6 {
if i == prev {
if repeat < roll_max[i as usize] as usize {
sum += Self::dfs(n - 1, i, repeat + 1, dp, roll_max);
sum %= M;
}
} else {
sum += Self::dfs(n - 1, i, 1, dp, roll_max);
sum %= M;
}
}
if prev >= 0 {
dp[prev as usize][repeat][n] = sum;
}
sum
}
}
}
#[test]
fn test() {
let n = 2;
let roll_max = vec![1, 1, 2, 2, 2, 3];
let res = 34;
assert_eq!(Solution::die_simulator(n, roll_max), res);
let n = 2;
let roll_max = vec![1, 1, 1, 1, 1, 1];
let res = 30;
assert_eq!(Solution::die_simulator(n, roll_max), res);
let n = 3;
let roll_max = vec![1, 1, 1, 2, 2, 3];
let res = 181;
assert_eq!(Solution::die_simulator(n, roll_max), res);
let n = 4;
let roll_max = vec![2, 1, 1, 3, 3, 2];
let res = 1082;
assert_eq!(Solution::die_simulator(n, roll_max), res);
}
// Accepted solution for LeetCode #1223: Dice Roll Simulation
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1223: Dice Roll Simulation
// class Solution {
// private Integer[][][] f;
// private int[] rollMax;
//
// public int dieSimulator(int n, int[] rollMax) {
// f = new Integer[n][7][16];
// this.rollMax = rollMax;
// return dfs(0, 0, 0);
// }
//
// private int dfs(int i, int j, int x) {
// if (i >= f.length) {
// return 1;
// }
// if (f[i][j][x] != null) {
// return f[i][j][x];
// }
// long ans = 0;
// for (int k = 1; k <= 6; ++k) {
// if (k != j) {
// ans += dfs(i + 1, k, 1);
// } else if (x < rollMax[j - 1]) {
// ans += dfs(i + 1, j, x + 1);
// }
// }
// ans %= 1000000007;
// return f[i][j][x] = (int) ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.