[LeetCode] Dice Roll Simulation

1223. 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] (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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
int dp[6][16][5001];
int mod = 1e9 + 7;
int helper(int n, int con, int num, vector<int>& ma) {
if(n == 0) return 1;
int& res = dp[num][con][n];
if(res != -1) return res;
res = 0;
for(int i = 0; i < 6; i++) {
if(i == num and ma[num] == con) continue;
res = (res + helper(n-1, i == num ? con + 1 : 1, i, ma)) % mod;

}
return res;
}
public:
int dieSimulator(int n, vector<int>& rollMax) {
memset(dp,-1,sizeof dp);
int res = 0;
for(int i = 0; i < 6; i++) {
res = (res + helper(n-1, 1, i, rollMax)) % mod;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/02/PS/LeetCode/dice-roll-simulation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.