当前位置:网站首页>Home raiding II (leetcode-213)
Home raiding II (leetcode-213)
2022-07-24 09:54:00 【Casten-Wang】
raid homes and plunder houses Ⅱ(LeetCode-213)
subject
You are a professional thief , Plan to steal houses along the street , There is a certain amount of cash in every room . All the houses in this place are Make a circle , This means that the first house and the last house are next to each other . meanwhile , Adjacent houses are equipped with interconnected anti-theft system , 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 device , The maximum amount you can steal tonight .
Example 1:
Input :nums = [2,3,2]
Output :3
explain : You can't steal first 1 House No ( amount of money = 2), And then steal 3 House No ( amount of money = 2), Because they are next to each other .
Example 2:
Input :nums = [1,2,3,1]
Output :4
explain : You can steal first 1 House No ( amount of money = 1), And then steal 3 House No ( amount of money = 3).
Maximum amount stolen = 1 + 3 = 4 .
Example 3:
Input :nums = [1,2,3]
Output :3
Tips :
1 <= nums.length <= 1000 <= nums[i] <= 1000
Ideas
When the number of rooms is no more than two , There is no need to consider the beginning and end
More than two hours , Because you can't steal both rooms at the same time , So we can consider it in two cases
- Don't steal the last case : The scope of theft n u m s [ 0 : n − 2 ] nums[0:n-2] nums[0:n−2]
- Don't steal the first case : The scope of theft n u m s [ 1 : n − 1 ] nums[1:n-1] nums[1:n−1]
The greater of the two
Code display
class Solution
{
public:
int rob(vector<int> &nums)
{
int n = nums.size();
if (n == 1)
{
return nums[0];
}
int left = robRange(nums, 0, n - 2);
int right = robRange(nums, 1, n - 1);
return max(left, right);
}
int robRange(vector<int> &nums, int start, int end)
{
if (start == end)
{
return nums[start];
}
vector<int> dp(nums.size());
dp[start] = nums[start];
dp[start + 1] = max(nums[start], nums[start + 1]);
for (int i = start + 2; i <= end; i++)
{
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[end];
}
};
边栏推荐
- The heads of the five major international institutions called for urgent action to deal with the global food security crisis
- CAS principle [concurrent programming]
- Is it safe for Oriental Fortune futures to open an online account, and will it be cheated?
- JS, the return statement used in the for loop should be placed in the function to terminate the loop, similar to the invalid return problem in break & foreach
- CRC Coding in C language
- [leetcode] 31. Next arrangement
- Spark Learning: how to choose different association forms and mechanisms?
- Linux deployment mysql8.0
- Arduino- use millis() to do two (or more) things at the same time
- Arduino drive Lora module master node
猜你喜欢

The heads of the five major international institutions called for urgent action to deal with the global food security crisis

CRC Coding in C language

It's eleven again. Those jokes about nagging programmers going home for blind dates

2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices

It is reported that the prices of some Intel FPGA chip products have increased by up to 20%

In the envy of LETV, there is a "meaning crisis" of contemporary workers
![[leetcode] 31. Next arrangement](/img/83/50a3cc17fc252582458bf32d1dd36b.png)
[leetcode] 31. Next arrangement
![[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)](/img/3b/981bd564a5855a317ccdd4800871ce.png)
[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)
![[don't bother with intensive learning] video notes (III) 1. What is SARS?](/img/5c/4636db2849ba8514976a5afaf56e38.png)
[don't bother with intensive learning] video notes (III) 1. What is SARS?

C#/VB. Net: convert word or EXCEL documents to text
随机推荐
PHP Basics - PHP super global variables
PHP Basics - session control - cookies
Spark Learning: using RDD API to implement inverted index
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus
How to improve office efficiency through online collaborative documents
Arduino drive Lora module node
Web page opening speed is very slow, how to solve it?
[example] v-contextmenu right click menu component
How does SRE and development of Google cooperate
Dorissql syntax Usage Summary
Friends come to interview a unicorn company in Beijing at leisure. The interview question is priced at 25K
Raspberry Pie:: no space left on device
Countdownlatch and join [concurrent programming]
cannot unpack non-iterable NoneType object
Problem: filesystemversionexception: you have version null and I want version 8
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display
Leetcode skimming: dynamic planning 03 (climb stairs with minimum cost)
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality
【笔记】什么是内核/用户空间 从CPU如何运行程序讲起
2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices