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;
}
相关推荐
爱吃提升10 分钟前
Dev-C++ 5.11 完整版快捷键大全(信奥/初学适用)
c语言·c++
杜 硕16 分钟前
快速排序02
算法·排序算法
careathers23 分钟前
【数据结构】二叉树拓展
数据结构·算法
java_nnnn31 分钟前
LeetCode刷题滑动窗口(长度最小的子数组)
算法·leetcode·职场和发展
@atweiwei34 分钟前
训练 AI 大模型系列之 LoRA:不动模型本体,靠一条低秩旁路改专长(附完整手算算例)
人工智能·算法·ai·语言模型·rag
小小龙学IT36 分钟前
FFmpeg 开源多媒体处理框架深度解析:从命令行到 libav* 全家桶
c语言·ffmpeg·开源
yi0111 小时前
DAY27: LeetCode 110. 平衡二叉树 | 在递归中判断是否平衡
人工智能·笔记·python·算法·leetcode·二叉树·递归
9624561 小时前
餐饮 SaaS 优惠券系统架构演进(七)营销定价引擎设计:活动、会员价、优惠券、买赠如何在同一 PriceQuote 中可解释地组合
java·运维·算法
m0_547486661 小时前
《数据结构:基于C语言的描述》全套PPT课件2026
c语言·数据结构
启友玩AI1 小时前
微安级唤醒、毫安级识别:基于 CI3422 的 AI 眼镜语音交互开发实战
c语言·人工智能·科技·嵌入式硬件·ai·ar·语音识别