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;
}
相关推荐
嵌入式 Linux手记9 小时前
柔性数组:为什么写 data[] 而不是 data[0]
linux·c语言·面试·职场和发展·嵌入式·柔性数组
青少儿编程课堂15 小时前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
郝学胜-神的一滴17 小时前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
geovindu17 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗19 小时前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
凌冰_19 小时前
数据结构 - 队列(五)
数据结构·算法·软件工程
让我们一起加油好吗20 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
让我们一起加油好吗21 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
Rain的Java大神之路21 小时前
LeetCode 42「接雨水」:面试官百出不厌的题,一文讲透从暴力到双指针 O(1)
java·后端·leetcode