当前位置:网站首页>leetcode:1997. 访问完所有房间的第一天【跳跃dp】
leetcode:1997. 访问完所有房间的第一天【跳跃dp】
2022-07-27 22:05:00 【白速龙王的回眸】

分析
必须偶数次才能前进
所以dp[i]记录第一次去到i
必须是第二次去到i - 1再走一步
所以如果说能去到i,必然前面都走了偶数次
那么我们就要探究dp[i]和dp[i - 1]的关系
i - 1 -> next[i - 1] -> i - 1 -> i
具体分析看注释
ac code
class Solution:
def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:
MOD = 10 ** 9 + 7
n = len(nextVisit)
dp = [0] * n # dp[i]表示第一次去到i的时间
dp[0] = 0
#
# i - 1 -> next[i - 1] -> i - 1 -> i
for i in range(1, n):
# i - 1 -> next[i - 1]
dp[i] = dp[i - 1] + 1
# core:到了i - 1,可以知道i - 1前面都是偶数次的
# 等价于next[i - 1] -> i - 1都没走
# 再走一次next[i - 1] -> i - 1
dp[i] += dp[i - 1] - dp[nextVisit[i - 1]]
# i - 1 -> i
dp[i] += 1
dp[i] %= MOD
return dp[-1] % MOD
总结
分奇偶的跳跃
注意next只能回跳
意味着i必须由i - 1来到
然后还有一个就是中间全偶数可以当作从来未跳(状态等价)
边栏推荐
- Jmeter 如何解决乱码问题?
- 592. Fraction addition and subtraction: introduction to expression calculation
- y79.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(十)
- Yangchuanhui, CTO of oceanbase: some HTAP databases are not real htaps
- From the second floor to the third floor
- 冲量在线出席2022数据要素安全流通论坛—政务领域专场,助力行业政务大数据建设创新发展
- leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
- Redis-事务与乐观锁
- JVM memory model
- 基本初等函数
猜你喜欢

Confused SCM matrix keys

Build Release Blogs

Basic operations of MySQL database (3) --- Based on fields

推进云网融合,筑路数字经济:英特尔亮相第五届数字中国建设峰会-云生态大会

Matlab | matlab terrain generation: rectangular iteration method, inverse Fourier transform method, fractal Berlin noise method

Basic operations of MySQL database (I) --- Based on Database

Point divide and conquer analysis

MATLAB | 那些你不得不知道的MATLAB小技巧(四)

MFC prompts that this application has requested the runtime to terminate it in an unused way editbox box has been deleted and is still in use

强强协同,共拓发展!英特尔与太一物联举办 AI 计算盒聚合服务研讨会
随机推荐
Fastjson历史漏洞复现
自动推理的逻辑09–自动定理证明
[book club issue 13] packaging format of audio and video files
Matlab | those matlab tips you have to know (3)
加拿大法院认定孟晚舟“双重犯罪”成立,引渡程序将继续进行!
点分治解析
公司7月来了个软件测试工程师,一副毛头小子的样儿,哪想到是新一代卷王...
Threejs personal notes
[BRE]软件构建发布自动化
MATLAB | 那些你不得不知道的MATLAB小技巧(一)
MATLAB | MATLAB地形生成:矩形迭代法 · 傅里叶逆变换法 · 分形柏林噪声法
592. Fraction addition and subtraction: introduction to expression calculation
【打新必读】魅视科技估值分析,分布式视听产品及解决方案
Redis learning and understanding of three special data types
How does JMeter solve the problem of garbled code?
递归求解迷宫问题
Harmonyos 3 pure mode can limit the access to personal data of risk applications detected in Huawei's application market
Leetcode 415. string addition and 43. string multiplication
Arm发布全新A78/G78/N78内核!还有支持自定义的Cortex-X系列CPU
Set data constructor