当前位置:网站首页>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
边栏推荐
- Vim使用Vundle安装代码补全插件(YouCompleteMe)
- Spark 算子之filter使用
- 【自娱自乐】构造笔记 week 2
- JS regular determines whether the port path of the domain name or IP is correct
- 多线程原理和常用方法以及Thread和Runnable的区别
- 下载并安装MongoDB
- Spark 算子之partitionBy
- 运维流程有多重要,听说一年能省下200万?
- MySQL - MySQL查询语句的执行过程
- Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
猜你喜欢
Read the meaning of serial port and various level signals
C language --- string + memory function
一文掌握vscode远程gdb调试
5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
新动态:SmartMesh和MeshBox的合作新动向
Government cloud migration practice: Beiming digital division used hypermotion cloud migration products to implement the cloud migration project for a government unit, and completed the migration of n
Function summary of drawing object arrangement in R language
C, calculation method and source program of bell number
volatile的含义以及用法
Install redis and deploy redis high availability cluster
随机推荐
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
捡起MATLAB的第(3)天
5 minutes, turn your excel into an online database, the magic cube net table Excel database
捡起MATLAB的第(2)天
Implement default page
GRBL学习(二)
leetcode-374 猜数字大小
C#,贝尔数(Bell Number)的计算方法与源程序
一文掌握vscode远程gdb调试
Unity shader learning
Algorithem_ ReverseLinkedList
Ice -- source code analysis
Spark 算子之partitionBy
Sortby use of spark operator
Config learning notes component
第九天 static 抽象类 接口
R语言中绘制ROC曲线方法二:pROC包
Install redis and deploy redis high availability cluster
运维流程有多重要,听说一年能省下200万?
Es common query, sorting and aggregation statements