当前位置:网站首页>Leetcode refers to offer II 089 House theft
Leetcode refers to offer II 089 House theft
2022-06-24 04:11:00 【freesan44】
subject
A professional thief , Plan to steal houses along the street . There is a certain amount of cash in every room , The only constraint on thieves' theft is that adjacent houses are equipped with interconnected anti-theft systems , If two adjacent houses are broken into by thieves on the same night , The system will automatically alarm .
Given an array of non negative integers representing the storage amount of each house nums , Please calculate Without triggering the alarm , The maximum amount that can be stolen overnight .
Example 1: Input :nums = [1,2,3,1] Output :4 explain : Steal 1 House No ( amount of money = 1) , And then steal 3 House No ( amount of money = 3). Maximum amount stolen = 1 + 3 = 4 . Example 2: Input :nums = [2,7,9,3,1] Output :12 explain : Steal 1 House No ( amount of money = 2), Steal 3 House No ( amount of money = 9), Then steal 5 House No ( amount of money = 1). Maximum amount stolen = 2 + 9 + 1 = 12 .
Tips :
1 <= nums.length <= 100
0 <= numsi <= 400
Their thinking
class Solution:
def rob(self, nums: [int]) -> int:
dp = [0]*len(nums)
# The boundary conditions
if len(nums) == 1:
return nums[0]
elif len(nums) == 2:
return max(nums[0],nums[1])
dp[0] = nums[0]
dp[1] = max(nums[0],nums[1])
for i in range(2, len(nums)):
dp[i] = max(dp[i-2]+nums[i],dp[i-1])
# print(dp)
return dp[-1]
if __name__ == "__main__":
nums = [2,7,9,3,1]
nums = [1, 2, 3, 1]
nums = [2,1,1,2]
ret = Solution().rob(nums)
print(ret)边栏推荐
- 脚本之美│VBS 入门交互实战
- 抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
- Optimization of digital transformation management of procurement platform in construction industry
- How can the new generation of HTAP databases be reshaped in the cloud? Tidb V6 online conference will be announced soon!
- Garbage collection mechanism
- JVM调优简要思想及简单案例-怎么调优
- Easygbs video playback protocol only webrtc can play. Troubleshooting
- 2021 graphic design trend: aesthetic response to chaos
- Configuration process of easygbs access to law enforcement recorder
- flutter系列之:flutter中的offstage
猜你喜欢

Clang代码覆盖率检测(插桩技术)

ServiceStack. Source code analysis of redis (connection and connection pool)

flutter系列之:flutter中的offstage

Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design

Kubernetes resource topology aware scheduling optimization

Black hat SEO actual combat search engine snapshot hijacking
![Web technology sharing | [map] to realize customized track playback](/img/b2/25677ca08d1fb83290dd825a242f06.png)
Web technology sharing | [map] to realize customized track playback

mysql - sql执行过程

Black hat SEO actual combat directory wheel chain generates millions of pages in batch

抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
随机推荐
博士申请 | 香港科技大学(广州)刘浩老师招收数据挖掘方向全奖博士/硕士
Several good books for learning data
What is a 1U server? What industries can 1U servers be used in?
In the post epidemic era, "cloud live broadcast" saves "cloud cultural tourism"?
How to intuitively explain server hosting and leasing to enterprises?
Difference and efficiency between get winevent and get eventlog
There is such a shortcut to learn a programming language systematically
Real time monitoring of water conservancy by RTU of telemetry terminal
多任务视频推荐方案,百度工程师实战经验分享
Oceanbase community OBD deployment example primary replica
Multi task video recommendation scheme, baidu engineers' actual combat experience sharing
Tsingsee Qingxi video easycvr integrated Dahua face recognition equipment
讲讲我的不丰富的远程办公经验和推荐一些办公利器 | 社区征文
系统的去学习一门编程语言,原来有如此捷径
High quality travel on national day, visual start of smart Tourism
Pine Script脚本常用内容
What is pseudo static? How to configure the pseudo static server?
How to be a web server and what are the advantages of a web server
Kubernetes resource topology aware scheduling optimization
"." in the structure of C language And "- & gt;" Differences between