当前位置:网站首页>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 边栏推荐
- MySQL数据库(二)DML数据操作语句和基本的DQL语句
- Stc-b learning board buzzer plays music
- Automated testing problems you must understand, boutique summary
- Investment should be calm
- 软件测试Bug报告怎么写?
- How to do agile testing in automated testing?
- ArrayList集合
- Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
- Maximum nesting depth of parentheses in leetcode simple questions
- Leetcode simple question: check whether two strings are almost equal
猜你喜欢

Portapack application development tutorial (XVII) nRF24L01 launch B

Winter vacation daily question - maximum number of balloons

How to become a good software tester? A secret that most people don't know

ucore lab8 文件系统 实验报告

Stc-b learning board buzzer plays music 2.0

How to rename multiple folders and add unified new content to folder names

线程及线程池

Capitalize the title of leetcode simple question

ucore lab2 物理内存管理 实验报告

安全测试入门介绍
随机推荐
What if software testing is too busy to study?
Portapack application development tutorial (XVII) nRF24L01 launch B
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
How to rename multiple folders and add unified new content to folder names
Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
Video scrolling subtitle addition, easy to make with this technique
The number of reversing twice in leetcode simple question
Public key box
HackTheBox-Emdee five for life
Leetcode simple question: check whether the numbers in the sentence are increasing
Mysql database (II) DML data operation statements and basic DQL statements
STC-B学习板蜂鸣器播放音乐2.0
Stc-b learning board buzzer plays music 2.0
Sorting odd and even subscripts respectively for leetcode simple problem
Future trend and planning of software testing industry
软件测试方法有哪些?带你看点不一样的东西
ucore lab2 物理内存管理 实验报告
Do you know the performance testing terms to be asked in the software testing interview?
ucore lab5用户进程管理 实验报告
Opencv recognition of face in image