当前位置:网站首页>Leetcode 198: house raiding
Leetcode 198: house raiding
2022-07-03 07:28:00 【Zhu Ya who can learn】
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 .

Ideas :
Five steps :
1. determine dp Array (dp table) And the meaning of subscripts
dp[i]: Consider Subscripts i( Include i) The houses within , The maximum amount that can be stolen is dp[i].
2. Determine the recurrence formula
decision dp[i] The most important factor is i Steal the room or not .
If you steal the first i room , that dp[i] = dp[i - 2] + nums[i] , namely : The first i-1 The house must not be considered , find Subscript i-2( Include i-2) The houses within , The maximum amount that can be stolen is dp[i-2] Add the number i The money stolen from the room .
If you don't steal the first i room , that dp[i] = dp[i - 1], Consider i-1 room ,( Note that this is about , It doesn't have to be stolen i-1 room , This is a confusing point for many students )
then dp[i] Taking the maximum , namely dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
3. dp How to initialize an array
From the recursive formula dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); It can be seen that , The basis of the recursive formula is dp[0] and dp[1]
from dp[i] By definition ,dp[0] It must be nums[0],dp[1] Namely nums[0] and nums[1] The maximum value of is :dp[1] = max(nums[0], nums[1]);
4. Determine the traversal order
dp[i] It's based on dp[i - 2] and dp[i - 1] Derived from , So it must be traversal from front to back !
5. deduction dp Array
// Dynamic programming
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[1] = Math.max(dp[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[nums.length - 1];
}
}边栏推荐
- C WinForm framework
- 【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
- PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
- Use of file class
- VMware virtual machine installation
- Pat grade a real problem 1166
- Leetcode 213: 打家劫舍 II
- 高并发内存池
- gstreamer ffmpeg avdec解码数据流向分析
- SecureCRT password to cancel session recording
猜你喜欢
![[solved] unknown error 1146](/img/f1/b8dd3ca8359ac9eb19e1911bd3790a.png)
[solved] unknown error 1146

Reconnaissance et détection d'images - Notes

7.2刷题两个
![PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef](/img/65/1f28071fc15e76abb37f1b128e1d90.jpg)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef

Understanding of class

Introduction of buffer flow

PAT甲级真题1166

Wireshark software usage

c语言指针的概念

Common methods of file class
随机推荐
La différence entre le let Typescript et le Var
Unified handling and interception of exception exceptions of vertx
HCIA notes
4279. Cartesian tree
The babbage industrial policy forum
Vertx metric Prometheus monitoring indicators
gstreamer ffmpeg avdec解码数据流向分析
2021-07-18
The difference between typescript let and VaR
IP home online query platform
List exercises after class
Talk about floating
Lombok -- simplify code
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
IO stream system and FileReader, filewriter
TCP cumulative acknowledgement and window value update
"Moss ma not found" solution
20220319
【已解决】win10找不到本地组策略编辑器解决方法