当前位置:网站首页>LeetCode#198. raid homes and plunder houses
LeetCode#198. raid homes and plunder houses
2022-07-06 15:21:00 【Rufeng ZHHH】
subject (LeetCode):
You are a professional thief , Plan to steal houses along the street . There is a certain amount of cash in every room , The only restriction on your 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 , Count you Without triggering the alarm , The maximum amount that can be stolen overnight .
Example 1:
Input :[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 :[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 .
The practice of this problem is dynamic programming , We can compare LeetCode#53. Maximum subarray and _ As the wind Zhhh The blog of -CSDN Blog
How to do it , You will find that they have many similarities , We also use dp[i] Before presentation i The maximum value obtained by item .
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums)==1: # List the special situations first ①
return nums[0]
elif len(nums)==2: # A special case ②
return max(nums)
dp=[0]*len(nums) # When len(nums)>2 when
dp[0]=nums[0] # If only 1 home , Get the value of the first house
dp[1]=max(nums[0],nums[1]) # The first two get the maximum of them
for i in range(2,len(nums)):
dp[i]=max(dp[i-1],dp[i-2]+nums[i]) # If the first i Home theft , Before getting i-1 Home maximum
return dp[len(nums)-1] # If the first i Home does not steal , Before getting i-2 Home maximum and number i Home value 边栏推荐
- UCORE lab1 system software startup process experimental report
- Threads et pools de threads
- If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
- 软件测试Bug报告怎么写?
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
- 软件测试有哪些常用的SQL语句?
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
- Face and eye recognition based on OpenCV's own model
- Servlet
- Dlib detects blink times based on video stream
猜你喜欢

ucore lab6 调度器 实验报告

想跳槽?面试软件测试需要掌握的7个技能你知道吗

CSAPP家庭作業答案7 8 9章
![Cadence physical library lef file syntax learning [continuous update]](/img/0b/75a4ac2649508857468d9b37703a27.jpg)
Cadence physical library lef file syntax learning [continuous update]

51 lines of code, self-made TX to MySQL software!

Interface test interview questions and reference answers, easy to grasp the interviewer

ucore lab7 同步互斥 实验报告
Automated testing problems you must understand, boutique summary

几款开源自动化测试框架优缺点对比你知道吗?

Investment operation steps
随机推荐
pytest
Differences between select, poll and epoll in i/o multiplexing
Automated testing problems you must understand, boutique summary
Winter vacation daily question - maximum number of balloons
基于485总线的评分系统双机实验报告
What if software testing is too busy to study?
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
ucore lab6 调度器 实验报告
Future trend and planning of software testing industry
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
几款开源自动化测试框架优缺点对比你知道吗?
ArrayList set
Threads et pools de threads
Mysql的事务
[200 opencv routines] 98 Statistical sorting filter
JDBC introduction
The latest query tracks the express logistics and analyzes the method of delivery timeliness
Introduction to variable parameters
What are the commonly used SQL statements in software testing?