当前位置:网站首页>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
边栏推荐
- 基于485总线的评分系统双机实验报告
- 自动化测试你必须要弄懂的问题,精品总结
- Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
- Dlib detects blink times based on video stream
- 软件测试工作太忙没时间学习怎么办?
- If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
- Mysql database (IV) transactions and functions
- In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
- Mysql database (II) DML data operation statements and basic DQL statements
- Nest and merge new videos, and preset new video titles
猜你喜欢
Description of Vos storage space, bandwidth occupation and PPS requirements
Future trend and planning of software testing industry
Your wechat nickname may be betraying you
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Lab 8 file system
Threads et pools de threads
MySQL数据库(四)事务和函数
线程及线程池
软件测试行业的未来趋势及规划
软件测试Bug报告怎么写?
随机推荐
MySQL development - advanced query - take a good look at how it suits you
Stc-b learning board buzzer plays music 2.0
ucore lab1 系统软件启动过程 实验报告
Capitalize the title of leetcode simple question
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
Lab 8 文件系统
Video scrolling subtitle addition, easy to make with this technique
Collection集合与Map集合
软件测试有哪些常用的SQL语句?
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
Sleep quality today 81 points
MySQL数据库(二)DML数据操作语句和基本的DQL语句
Mysql database (III) advanced data query statement
ucore lab8 文件系统 实验报告
Thinking about three cups of tea
51 lines of code, self-made TX to MySQL software!
Eigen User Guide (Introduction)
A method and implementation of using VSTO to prohibit excel cell editing
Interview answering skills for software testing
[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)