当前位置:网站首页>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];
}
边栏推荐
- win10清除快速访问-不留下痕迹
- SQL injection - injection based on MSSQL (SQL Server)
- Lightroom import picture gray / Black rectangular multi display
- 每周小结(*63):关于正能量
- Excel comparator
- Nexus 6p从8.0降级6.0+root
- px em rem的区别
- JS get the attribute values nested in the object
- Practical gadget instructions
- 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
猜你喜欢
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
Component、Container容器常用API详解:Frame、Panel、ScrollPane
Configure cross compilation tool chain and environment variables
ES6 modularization
Gridview出现滚动条,组件冲突,如何解决
如何避免 JVM 内存泄漏?
BUU-Crypto-Cipher
What are the reasons for the frequent high CPU of ECS?
随机推荐
Configure cross compilation tool chain and environment variables
gslb(global server load balance)技术的一点理解
Basic concept of bus
C语言练习题(递归)
Nexus 6p downgraded from 8.0 to 6.0+root
Practical gadget instructions
724. Find the central subscript of the array
Introduction to AMBA
C语言中的函数(详解)
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
(4) Canal multi instance use
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
如何获取el-tree中所有节点的父节点
js arguments参数使用和详解
left_ and_ right_ Net normal version
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
win10清除快速访问-不留下痕迹
Install pytoch geometric
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout