当前位置:网站首页>力扣-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
边栏推荐
- ES常用查询、排序、聚合语句
- WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
- Upgrade MySQL 5.1 to 5.610
- Simple usage of dlopen / dlsym / dlclose
- Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
- 【现代电子装联期末复习要点】
- C#,贝尔数(Bell Number)的计算方法与源程序
- How important is the operation and maintenance process? I heard it can save 2 million a year?
- 一文掌握vscode远程gdb调试
- Config learning notes component
猜你喜欢
Nanny Anaconda installation tutorial
Timing model: gated cyclic unit network (Gru)
Distinct use of spark operator
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
捡起MATLAB的第(7)天
Spark 算子之distinct使用
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
Unity shader learning
Master vscode remote GDB debugging
matplotlib教程05---操作图像
随机推荐
New developments: new trends in cooperation between smartmesh and meshbox
腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上
homwbrew安装、常用命令以及安装路径
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
Filter usage of spark operator
PS add texture to picture
dlopen/dlsym/dlclose的简单用法
Go language, condition, loop, function
JS regular détermine si le nom de domaine ou le chemin de port IP est correct
Best practices of Apache APIs IX high availability configuration center based on tidb
【第5节 if和for】
One brush 313 sword finger offer 06 Print linked list from end to end (E)
Timing model: gated cyclic unit network (Gru)
ESP32编译环境的搭建
Grbl learning (II)
Read the meaning of serial port and various level signals
gps北斗高精度卫星时间同步系统应用案例
通过Feign在服务之间传递header请求头信息
Install redis and deploy redis high availability cluster