当前位置:网站首页>Force buckle - 198 raid homes and plunder houses
Force buckle - 198 raid homes and plunder houses
2022-04-23 16:06:00 【Node_ Su】
dp[i] Indicates that the robbery has reached the current i Then you can steal the maximum amount of the first three
First , front 3 Home special treatment
then , Whether to rob the current home should consider robbing i-2 Jiahe i-3 Whether the maximum benefit can be achieved , Because you can't rob your neighbors i-1
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
dp = [0] * n # The initial wallet is 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://yzsam.com/2022/04/202204231603458118.html
边栏推荐
猜你喜欢
MySQL - MySQL查询语句的执行过程
Spark 算子之coalesce与repartition
Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
Unity shader learning
Interview question 17.10 Main elements
Spark 算子之partitionBy
Research and Practice on business system migration of a government cloud project
一文读懂串口及各种电平信号含义
Spark 算子之distinct使用
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
随机推荐
捡起MATLAB的第(10)天
Filter usage of spark operator
【现代电子装联期末复习要点】
贫困的无网地区怎么有钱建设网络?
VIM uses vundle to install the code completion plug-in (youcompleteme)
保姆级Anaconda安装教程
Master vscode remote GDB debugging
Spark 算子之交集、并集、差集
Passing header request header information between services through feign
Ice -- source code analysis
Day (7) of picking up matlab
Research and Practice on business system migration of a government cloud project
Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Day (10) of picking up matlab
Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
How important is the operation and maintenance process? I heard it can save 2 million a year?
Day (4) of picking up matlab
Day (3) of picking up matlab
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope