当前位置:网站首页>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来到
然后还有一个就是中间全偶数可以当作从来未跳(状态等价)
边栏推荐
- Arm发布全新A78/G78/N78内核!还有支持自定义的Cortex-X系列CPU
- Harmonyos 3 pure mode can limit the access to personal data of risk applications detected in Huawei's application market
- 有趣的哈夫曼树
- leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
- Basic elementary function
- Microsoft Amazon layoffs, the economic crisis is getting closer...
- Is it amazing to extract text from pictures? Try three steps to realize OCR!
- 自动推理的逻辑09–自动定理证明
- MATLAB | MATLAB地形生成:矩形迭代法 · 傅里叶逆变换法 · 分形柏林噪声法
- 程序员工作中的理性与感性活动及所需的技能素养
猜你喜欢

map集合

Buildforge materials

The program design questions of the 11th national competition of Bluebridge cup single chip microcomputer

Rational and perceptual activities and required skills in programmers' work

蓝桥杯单片机第十一届国赛程序设计试题

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

冲量在线出席2022数据要素安全流通论坛—政务领域专场,助力行业政务大数据建设创新发展

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

图片提取文字很神奇?试试三步实现OCR!

Matlab | matlab terrain generation: rectangular iteration method, inverse Fourier transform method, fractal Berlin noise method
随机推荐
ADB path cannot contain 2 spaces remote could n't create file: is a directory
英特尔AI实践日第56期 | 探讨行业发展新趋势
mysql数据库的基本操作(三)-——基于字段
Possible reasons why there is no voltage in the corresponding channel, but the ADC value is changing greatly and is not equal to 0
Leetcode 415. string addition and 43. string multiplication
Redis learning and understanding of three special data types
Leetcode 452. minimum number of arrows to burst balloons (medium)
See how well-known enterprises use Web3 to reshape their industries
OpenVINO整合TensorFlow实现推理加速
ASML推出第一代HMI多光束检测机:速度提升600%,适用于5nm及更先进工艺
How to realize fast recognition of oversized images
公司7月来了个软件测试工程师,一副毛头小子的样儿,哪想到是新一代卷王...
Jmeter 如何解决乱码问题?
英特尔发布开源AI参考套件
MATLAB | 那些你不得不知道的MATLAB小技巧(二)
Solve maze problem recursively
Applet helps smart home ecological platform
递归求解迷宫问题
网络设备硬核技术内幕 防火墙与安全网关篇 (小结)
有趣的哈夫曼树