当前位置:网站首页>1223. Dice simulation range DP
1223. Dice simulation range DP
2022-07-25 23:50:00 【Empress Yu】
1223. Roll the dice to simulate
There is a dice simulator that generates a... Every time you roll 1 To 6 The random number .
But we have a constraint when using it , Is to make the dice roll , continuity Throw a number
iYou can't do more thanrollMax[i](ifrom 1 Numbered starting ).Now? , Give you an array of integers
rollMaxAnd an integern, Please count the rollnThe number of different point sequences that can be obtained from one dice .If at least one element in two sequences is different , I think the two sequences are different . Because the answer can be big , So please return to model
10^9 + 7Later results .Example 1:
Input :n = 2, rollMax = [1,1,2,2,2,3] Output :34 explain : We throw 2 Second dice , If there are no constraints , share 6 * 6 = 36 A possible combination . But according to rollMax Array , Numbers 1 and 2 At most once in a row , So there will be no sequence (1,1) and (2,2). therefore , The final answer is 36-2 = 34.Example 2:
Input :n = 2, rollMax = [1,1,1,1,1,1] Output :30Example 3:
Input :n = 3, rollMax = [1,1,1,2,2,3] Output :181Tips :
1 <= n <= 5000rollMax.length == 61 <= rollMax[i] <= 15
source : Power button (LeetCode)
link :https://leetcode.cn/problems/dice-roll-simulation
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The result of doing the question
Failure , This type of dp It feels particularly difficult , I can't find the feeling after writing several times , The foundation of mathematics may be more important
Method : Dynamic programming
Reference resources :https://leetcode.cn/problems/dice-roll-simulation/solution/java-2wei-dp-by-zdxiq125/
1. Suppose the number of quantity elements in front is insufficient , There is no need to deal with , Throw all the previous results directly to 1 To 6 Accumulation
2. Suppose you just step on the edge of the limit , For example, there can only be 3 individual 3, Now it happens to be 4 Number , Then remove the first one 3 The situation of ,3 3 3 3
3. Suppose there is 5 Number , b a 3 3 3, that a It can be any other number , But it can't be 3, stay a No 3 The situation of , Push to the first 3, In order to properly remove , Because this is the only case ,a 3 3 3, First of all 3 Can guarantee to be the first , Arbitrary non 3 Add a after the number 3, Can guarantee this 3 It is the first of this continuous segment 3, Note that we cannot start directly from the first 3 Start pushing , Because this and may contain this 3 Is the first , the second 3 The situation of , These situations are legal . So this part needs to be preceded by a number non 3 And derived .
class Solution {
public int dieSimulator(int n, int[] rollMax) {
long MOD = (long) (1e9+7);
long[][] dp = new long[n+1][6];
Arrays.fill(dp[1],1);
for(int i = 2; i <= n; i++){
for(int j=0; j < 6; j++){
for(int k = 0; k < 6; k++){
dp[i][j] = (dp[i][j]+dp[i-1][k])%MOD;
}
if(i-rollMax[j]-1==0){
--dp[i][j];
}else if(i-rollMax[j]-1>0){
for(int k = 0; k < 6; k++){
if(k!=j){
dp[i][j] = (dp[i][j]-dp[i-rollMax[j]-1][k]+MOD)%MOD;
}
}
}
}
}
long sum = 0;
for(int i = 0; i < 6; i++){
sum += dp[n][i];
sum = sum %MOD;
}
return (int)sum;
}
}边栏推荐
- S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
- Data intensive application system design - Application System Overview
- Scroll series
- 1913. Maximum product difference between two number pairs - no sorting required
- [wechat applet] page navigation
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
- Wrote a little webapi knowledge points from 0 to 1
- 下一代终端安全管理的关键特征与应用趋势
- 图的遍历-DFS,BFS(代码详解)
猜你喜欢

什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。

C language (high level) program environment and preprocessing

Generating random number random learning uniform_ int_ distribution,uniform_ real_ distribution

利用用户脚本优化 Yandere/Konachan 站点浏览体验

Vscode format JSON file

死信队列 和消息TTL过期代码

Loading process such as reflection

浅识 OWASP

Find the cause of program dead cycle through debugging

开放API生态系统面临的十个威胁
随机推荐
Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
Learning exploration - waves
[code case] blog page design (with complete source code)
C# - readonly 和 const 关键字
2022 Niuke multi School Game 2
762. Prime number calculation setting in binary representation
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
What is parity? How to use C language?
ABAP 代码中读取会计科目的字段状态(隐藏、可选、必输)
死信队列 和消息TTL过期代码
Nacos 下线服务时报错 errCode: 500
sftp和ftp的区别
[learning notes] unreal 4 engine introduction (IV)
Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
Native JS perfectly realizes deep copy
A brief introduction to OWASP
S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)
arcgis根据矢量范围裁取tif影像(栅格数据)、批量合并shp文件、根据矢量范围裁取区域内的矢量,输出地理坐标系、转换16位TIF影像的像素深度至8位、shp文件创建和矢量框标绘设置
C language implementation of three chess
Imitating the magnifying glass effect of JD products -- JS Foundation