当前位置:网站首页>Looting (leetcode-198)
Looting (leetcode-198)
2022-07-24 09:54:00 【Casten-Wang】
raid homes and plunder houses (LeetCode-198)
subject
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 .
Tips :
1 <= nums.length <= 1000 <= nums[i] <= 400
Ideas
Five steps
dp[i]meaning- Before stealing i Houses ( Including house i) The maximum amount that can be obtained
The recursive formula
- d p [ i ] = m a x ( d p [ i − 1 ] , d p [ i − 2 ] + n u m s [ i ] ) dp[i]=max(dp[i-1],dp[i-2]+nums[i]) dp[i]=max(dp[i−1],dp[i−2]+nums[i])
Array initialization
dp[0]=0 dp[1]=nums[0]
traversal order
- Before and after
Code
class Solution
{
public:
int rob(vector<int> &nums)
{
int n = nums.size();
if (n == 1)
{
return nums[0];
}
if (n == 2)
{
return max(nums[0], nums[1]);
}
vector<int> dp(2);
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
int result;
for (int i = 2; i < n; i++)
{
result = max(dp[1], dp[0] + nums[i]);
dp[0] = dp[1];
dp[1] = result;
}
return result;
}
};
边栏推荐
- 2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
- [Luogu p5829] [template] mismatch tree (string) (KMP)
- An article takes you to understand the operation of C language files in simple terms
- Problem: filesystemversionexception: you have version null and I want version 8
- Android Version Description security privacy 13
- JS 84*148=b6a8 how many decimal places can you make both sides equal
- unity中物体z旋转同步面板上的数值
- Mysql8.0 authorized remote login
- In the envy of LETV, there is a "meaning crisis" of contemporary workers
- Boundless dialogue | participate in the live broadcast on July 25 and win the prize
猜你喜欢

Spark Learning: a form of association in a distributed environment?
![[leetcode] 31. Next arrangement](/img/83/50a3cc17fc252582458bf32d1dd36b.png)
[leetcode] 31. Next arrangement

Leetcode skimming: dynamic planning 03 (climb stairs with minimum cost)

Spark Learning: compile spark source code in win10
![[STM32 learning] (22) STM32 realizes 360 degree rotary encoder](/img/8e/fb036296ec3aff5e60acee5018943c.png)
[STM32 learning] (22) STM32 realizes 360 degree rotary encoder
![[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus](/img/6b/b49b9e7fff62026a4818561d79fe3f.png)
[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus

Cloud primordial (12) | introduction to kubernetes foundation of kubernetes chapter

Vscode failed to use SSH Remote Connection (and a collection of other problems)

Spark Learning: Spark implementation of distcp
![[STM32 learning] (10) stm32f1 general timer realizes pulse counter](/img/42/1f5104f923b1868dda23bcc425fc62.png)
[STM32 learning] (10) stm32f1 general timer realizes pulse counter
随机推荐
Spark Learning: compile spark source code in win10
Lung CT segmentation challenge 2017 dataset download and description
Basic knowledge of PHP - complete collection of PHP functions
财务数字化转型
Scala learning: why emphasize immutable objects?
Hucang integrated e-commerce project (I): introduction to the project background and structure
Compilation and linking of programs
What are the 6% annualized products?
Write a simple memo using localstorage
Hands on deep learning (VII) -- bounding box and anchor box
[STM32 learning] (9) stm32f1 general timer realizes simple breathing lamp
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)
PHP Basics - session control - cookies
[STM32 learning] (22) STM32 realizes 360 degree rotary encoder
[don't bother with intensive learning] video notes (III) 1. What is SARS?
获取所有股票历史行情数据
[STM32 learning] (8) stm32f1 general timer configuration
PHP caching system - PHP uses Memcache
Financial digital transformation