当前位置:网站首页>198. House raiding
198. House raiding
2022-07-04 06:01:00 【mrbone9】
Address :
Power button https://leetcode-cn.com/problems/house-robber/ 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 <= 100 0 <= nums[i] <= 400 |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/house-robber
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Adopt the idea of dynamic programming
Suppose the current house is i, that dp[i] Indicates the total amount of the stolen house
dp[i] Your choice depends on :
1. Current house selection ,i-1 You can't choose a house ,dp[i] = dp[i-2] + nums[i]
2. The current house is not selected , It depends on dp[i-1] Value
Maximization is max(dp[i-1], dp[i-2] + nums[i])
Initial value :
dp[0] = nums[0] No choice
dp[1] = max(nums[0], nums[1]) A choice
Method 1 、 Dynamic programming
#define max(a,b) ( (a) > (b) ? (a) : (b) )
int rob(int* nums, int numsSize){
int i;
int dp[101]={0};
dp[0] = nums[0];
for(i=1; i<numsSize; i++)
{
if(i == 1)
dp[i] = max(nums[0], nums[1]);
else
dp[i]= max(dp[i-1], dp[i-2]+nums[i]);
}
return dp[numsSize - 1];
}
边栏推荐
- Descriptive analysis of data distribution characteristics (data exploration)
- BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
- 卸载Google Drive 硬盘-必须退出程序才能卸载
- Arc135 a (time complexity analysis)
- Lightroom import picture gray / Black rectangular multi display
- Use of hutool Pinyin tool
- Halcon image calibration enables subsequent image processing to become the same as the template image
- JS execution mechanism
- 实用的小工具指令
- buuctf-pwn write-ups (8)
猜你喜欢
C language exercises (recursion)
Design and implementation of redis 7.0 multi part AOF
buuctf-pwn write-ups (8)
How to expand all collapse panels
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
Sword finger offer II 038 Daily temperature
19. Framebuffer application programming
随机推荐
Excel 比较日器
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
Impact relay jc-7/11/dc110v
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
ES6 modularization
Configure cross compilation tool chain and environment variables
Notes and notes
Nexus 6p downgraded from 8.0 to 6.0+root
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
Install pytoch geometric
el-select如何实现懒加载(带搜索功能)
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
Accidentally deleted the data file of Clickhouse, can it be restored?
Online shrimp music will be closed in January next year. Netizens call No
buuctf-pwn write-ups (8)
Arc135 a (time complexity analysis)
How to determine whether an array contains an element
C语言练习题(递归)
Thinkphp6.0 middleware with limited access frequency think throttle
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。