当前位置:网站首页>leetcode:6107. 不同骰子序列的数目【dp六个状态 + dfs记忆化】
leetcode:6107. 不同骰子序列的数目【dp六个状态 + dfs记忆化】
2022-06-26 21:39:00 【白速龙王的回眸】
分析
对于记忆化搜索,一定是固定参数的dfs的值是一样的,所以要想好设置什么参数和返回什么值
这里的话,参数可以设置n长度,last上一个 和last2上上个
值的话就设置为不同的序列数
特别的,当n是0的时候,没得选也算一种
注意,初始值可以设置为7,7(两个last)说明可以任选
这里我们的函数意思是# f:后面还有n个,前面两个分别是last和last2
dfs + cache
# f:后面还有n个,前面两个分别是last和last2
@cache
def f(n: int, last: int, last2: int) -> int:
#print(n, last, last2)
# 退出条件:后面没有了(没有了就是一种选法)
if n == 0: return 1
res = 0
for j in range(1, 7): # 从第一位开始选
if j != last and j != last2 and gcd(j, last) == 1:
res += f(n - 1, j, last)
return res % (10 ** 9 + 7)
class Solution:
def distinctSequences(self, n: int) -> int:
return f(n, 7, 7) # 7 与 [1,6] 内的数字都不同且互质
dp
每个值维护6个状态(以这六个状态作为结尾的个数),前三个先手写出来
从第四个开始,选定了dp[i][j]后先把所有能选的dp[i - 1][k]加起来,由于last2和j不能相同,所以要减去dp[i - 2][j]
注意到如果i-2选择j的话,实际上i - 1是有dp[2][j]中选法的,但是由于i - 1和i - 3不能相同,导致i - 1只有dp[2][j] - 1中选法 比较绕
举例:dp[i][0] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][5] - dp[i - 2][0] * (dp[2][0] - 1)
dp code
class Solution:
def distinctSequences(self, n: int) -> int:
MOD = 10 ** 9 + 7
if n == 1:
return 6
if n == 2:
return 22
if n == 3:
return 66
dp = [[0] * 6 for _ in range(n + 1)]
dp[1] = [1] * 6
dp[2] = [5, 3, 4, 3, 5, 2]
dp[3] = [12, 11, 12, 11, 12, 8]
#mapp = {1:}
for i in range(4, n + 1):
dp[i][0] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][5] - dp[i - 2][0] * (dp[2][0] - 1)
dp[i][1] = dp[i - 1][0] + dp[i - 1][2] + dp[i - 1][4] - dp[i - 2][1] * (dp[2][1] - 1)
dp[i][2] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][4] - dp[i - 2][2] * (dp[2][2] - 1)
dp[i][3] = dp[i - 1][0] + dp[i - 1][2] + dp[i - 1][4] - dp[i - 2][3] * (dp[2][3] - 1)
dp[i][4] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][5] - dp[i - 2][4] * (dp[2][4] - 1)
dp[i][5] = dp[i - 1][0] + dp[i - 1][4] - dp[i - 2][5] * (dp[2][5] - 1)
#print(dp)
return sum(dp[-1]) % MOD
总结
dfs + cache很优雅,要想好dfs的含义,以及每个参数的意义,和返回值的结果,当我们固定了参数返回值就应该是固定的
dp + 多状态很有意思,通过以哪个结尾分成六种dp,然后dp间互相作用(前三个),关系很复杂。。
边栏推荐
- Dynamic parameter association using postman
- YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again
- PostgreSQL notes
- Leetcode question brushing: String 06 (implement strstr())
- 诗尼曼家居冲刺A股:年营收近12亿 红星美凯龙与居然之家是股东
- DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力
- MacOS环境下使用HomeBrew安装[email protected]
- 茂莱光学科创板上市:拟募资4亿 范一与范浩兄弟为实控人
- Student information management system based on SSH Framework
- Leetcode: hash table 08 (sum of four numbers)
猜你喜欢
CVPR 2022 | 美团技术团队精选论文解读
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
YOLOv6:又快又准的目标检测框架开源啦
Redis + Guava 本地缓存 API 组合,性能炸裂!
[LeetCode]-链表-2
About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
[Bayesian classification 3] semi naive Bayesian classifier
Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
回首望月
Treasure and niche cover PBR multi-channel mapping material website sharing
随机推荐
fastadmin极光推送发送消息的时候registration_id多个用逗号分割后无效
DLA模型(分类模型+改进版分割模型) + 可变形卷积
Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)
12个MySQL慢查询的原因分析
不要做巨嬰了
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
【连载】说透运维监控系统01-监控系统概述
Chapter 2 construction of self defined corpus
在哪个平台买股票开户最安全?求分享
Leetcode(452)——用最少数量的箭引爆气球
SAP Spartacus 中的依赖注入 Dependency Injection 介绍
Is there any risk in registering and opening an account for stock speculation? Is it safe?
基于Qt实现的“合成大西瓜”小游戏
亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
Establish a connection with MySQL
Twenty five of offer - all paths with a certain value in the binary tree
leetcode刷题:字符串02( 反转字符串II)
AI intelligent matting tool - hair can be seen
Introduction of classic wide & deep model and implementation of tensorflow 2 code