当前位置:网站首页>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
边栏推荐
- Maximum nesting depth of parentheses in leetcode simple questions
- China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
- Threads and thread pools
- What to do when programmers don't modify bugs? I teach you
- UCORE lab8 file system experiment report
- pytest
- Example 071 simulates a vending machine, designs a program of the vending machine, runs the program, prompts the user, enters the options to be selected, and prompts the selected content after the use
- Mysql database (II) DML data operation statements and basic DQL statements
- 自动化测试你必须要弄懂的问题,精品总结
- CSAPP homework answers chapter 789
猜你喜欢
CSAPP shell lab experiment report
How to become a good software tester? A secret that most people don't know
UCORE lab2 physical memory management experiment report
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
ucore lab5
Maximum nesting depth of parentheses in leetcode simple questions
Word macro operation: convert the automatic number in the document into editable text type
Interface test interview questions and reference answers, easy to grasp the interviewer
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
线程及线程池
随机推荐
Nest and merge new videos, and preset new video titles
遇到程序员不修改bug时怎么办?我教你
Should wildcard import be avoided- Should wildcard import be avoided?
Report on the double computer experiment of scoring system based on 485 bus
UCORE LaB6 scheduler experiment report
Programmers, how to avoid invalid meetings?
Introduction to variable parameters
Differences between select, poll and epoll in i/o multiplexing
Stc-b learning board buzzer plays music 2.0
Portapack application development tutorial (XVII) nRF24L01 launch B
Sleep quality today 81 points
Global and Chinese markets for complex programmable logic devices 2022-2028: Research Report on technology, participants, trends, market size and share
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
Introduction to safety testing
软件测试有哪些常用的SQL语句?
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
How to become a good software tester? A secret that most people don't know
Do you know the performance testing terms to be asked in the software testing interview?
MySQL数据库(一)
ArrayList set