当前位置:网站首页>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)边栏推荐
- Go operation mongodb
- Analysis of grafana SSO authentication process based on keyloak
- What is pseudo static? How to configure the pseudo static server?
- What should I pay attention to when choosing a data center?
- Structure size calculation of C language struct
- 我与物联有个约定
- Pine Script脚本常用内容
- Cadence OrCAD Capture 批量修改网络名称的两种最实用的方法图文教程及视频演示
- LeetCode 938. Range sum of binary search tree
- uni-app进阶之认证【day12】
猜你喜欢

openGauss 3.0版本源码编译安装指南

Common content of pine script script

Pine Script脚本常用内容

共建欧拉社区 共享欧拉生态|携手麒麟软件 共创数智未来

SQL注入绕过安全狗思路一

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

多任务视频推荐方案,百度工程师实战经验分享

The results of the 2022 open source summer were announced, and 449 college students will contribute to open source projects

黑帽SEO实战之目录轮链批量生成百万页面

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
随机推荐
讲讲我的不丰富的远程办公经验和推荐一些办公利器 | 社区征文
C language in DSP (2) -- definition of structure
LeetCode 2006. Number of pairs whose absolute value of difference is k
The collection method of penetration test, and which methods can be used to find the real IP
微博国际版更名为微博轻享版
flutter系列之:flutter中的offstage
How to remote server is the price of the server expensive
From virtual to real, digital technology makes rural funds "live"
Why is on-line monitoring of equipment more and more valued by people?
黑帽SEO实战之通用301权重pr劫持
Demonstration of the processing of divisor 0 in SQL
Life reopens simulation / synthetic big watermelon / small air conditioner Inventory of 2021 popular open source projects
多任务视频推荐方案,百度工程师实战经验分享
What is FTP? What is the FTP address of the ECS?
Structure size calculation of C language struct
What is a 1U server? What industries can 1U servers be used in?
Wide & deep model and optimizer understand code practice
Kubernetes 资源拓扑感知调度优化
Multi task video recommendation scheme, baidu engineers' actual combat experience sharing
What is FTP? How does the ECS open the FTP protocol?