LeetCode //C - 1223. 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 rollMaxi (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 10 9 + 7 10^9 + 7 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 <= 5000
  • rollMax.length == 6
  • 1 <= rollMaxi <= 15

From: LeetCode

Link: 1223. Dice Roll Simulation


Solution:

Ideas:

DP by ending number and consecutive count.

Code:
c 复制代码
int dieSimulator(int n, int* rollMax, int rollMaxSize) {
    const int MOD = 1000000007;
    
    // dp[j][k]: sequences ending with number j, repeated k times
    long long dp[6][16] = {0};
    
    for (int j = 0; j < 6; j++) {
        dp[j][1] = 1;
    }
    
    for (int len = 2; len <= n; len++) {
        long long next[6][16] = {0};
        
        for (int last = 0; last < 6; last++) {
            for (int cnt = 1; cnt <= rollMax[last]; cnt++) {
                if (dp[last][cnt] == 0) continue;
                
                for (int x = 0; x < 6; x++) {
                    if (x == last) {
                        if (cnt + 1 <= rollMax[x]) {
                            next[x][cnt + 1] = (next[x][cnt + 1] + dp[last][cnt]) % MOD;
                        }
                    } else {
                        next[x][1] = (next[x][1] + dp[last][cnt]) % MOD;
                    }
                }
            }
        }
        
        for (int j = 0; j < 6; j++) {
            for (int k = 1; k <= 15; k++) {
                dp[j][k] = next[j][k];
            }
        }
    }
    
    long long ans = 0;
    for (int j = 0; j < 6; j++) {
        for (int k = 1; k <= rollMax[j]; k++) {
            ans = (ans + dp[j][k]) % MOD;
        }
    }
    
    return (int)ans;
}
相关推荐
171320330计算机毕设编程13 分钟前
2027计算机毕设五大方向对比&选题推荐
java·ide·python·算法·django·php·推荐算法
Tisfy17 分钟前
LeetCode 2058.找出临界点之间的最小和最大距离:遍历+遇到极值则更新(这种题谁空间复杂度不是O(1)啊)
linux·数据库·leetcode·链表·题解·模拟·遍历
万物智能26 分钟前
设备树DTS-【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
后端·算法
代码不停33 分钟前
子数组问题
java·算法
小欣加油42 分钟前
Leetcode2058 找出临界点之间的最小和最大距离
数据结构·c++·算法·leetcode·职场和发展
Patrick在香港1 小时前
Python 拉取 C&SD 官方 API:香港 2022 年已跨过“超老龄线“,而抚养比正在爬回 1961
android·c语言·python·数据分析·时序数据库·数据可视化·香港
华华哥20221 小时前
《模型不玄学》第25章 双头模型:共享底座 + 两个分支
算法
Yfhonier1 小时前
6441. 特别的除法
c++·算法
华华哥20221 小时前
《模型不玄学》第5章 看懂你的数据:先认字段,再过门禁
算法