当前位置:网站首页>力扣-198.打家劫舍
力扣-198.打家劫舍
2022-04-23 16:03:00 【Node_Su】
dp[i]表示打劫到当前第 i 家的最大金额
首先,前3家特殊处理
然后,是否打劫当前家应考虑打劫 i-2 家和 i-3 是否能达到最大收益,因为不能打劫邻居 i-1
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
dp = [0] * n # 初始钱包为0
dp[0] = nums[0]
if n == 1:
return nums[0]
if n == 2:
dp[1] = max(nums[0], nums[1])
return dp[1]
if n >= 3:
dp[1] = max(nums[0], nums[1])
dp[2] = max(nums[0] + nums[2], nums[1])
for i in range(3, n):
dp[i] = max(dp[i - 2] + nums[i], dp[i - 3] + nums[i])
res = -1
for i in range(n):
res = max(dp[i], res)
return res
if __name__ == '__main__':
nums = [1, 2, 1, 1]
Sol = Solution()
res = Solution.rob(Sol, nums)
print(res)
版权声明
本文为[Node_Su]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Node_Su/article/details/124363035
边栏推荐
- JS regular détermine si le nom de domaine ou le chemin de port IP est correct
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- Spark 算子之partitionBy
- Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
- Implement default page
- Go language, condition, loop, function
- shell_ two
- Ice -- source code analysis
- js正则判断域名或者IP的端口路径是否正确
- ESP32_Arduino
猜你喜欢
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
C, calculation method and source program of bell number
CVPR 2022 quality paper sharing
C language --- advanced pointer
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
Do we media make money now? After reading this article, you will understand
腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
volatile的含义以及用法
Intersection, union and difference sets of spark operators
随机推荐
Read the meaning of serial port and various level signals
Calculate the number of occurrences of a character
Jour (9) de ramassage de MATLAB
ESP32_Arduino
GRBL学习(一)
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
Passing header request header information between services through feign
Spark 算子之coalesce与repartition
String sorting
C language --- advanced pointer
ESP32编译环境的搭建
运维流程有多重要,听说一年能省下200万?
TIA botu - basic operation
Win11 / 10 home edition disables the edge's private browsing function
Grbl learning (II)
C#,贝尔数(Bell Number)的计算方法与源程序
Nanny Anaconda installation tutorial
ESP32_ Arduino
ES常用查询、排序、聚合语句
Named in pytoch_ parameters、named_ children、named_ Modules function