当前位置:网站首页>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];
}
}边栏推荐
- Jeecg menu path display problem
- Read config configuration file of vertx
- IP home online query platform
- [set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
- Jeecg request URL signature
- [set theory] order relation (partial order relation | partial order set | example of partial order set)
- twenty million two hundred and twenty thousand three hundred and nineteen
- IO stream system and FileReader, filewriter
- PAT甲级真题1166
- Introduction of buffer flow
猜你喜欢

Comparison of advantages and disadvantages between most complete SQL and NoSQL

VMWare网络模式-桥接,Host-Only,NAT网络
![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

Common APIs

Understanding of class

Summary of abnormal mechanism of interview

docker建立mysql:5.7版本指定路径挂载不上。

Spa single page application

Pat grade a real problem 1166

SecureCRT password to cancel session recording
随机推荐
Advanced API (multithreading)
Why is data service the direction of the next generation data center?
你开发数据API最快多长时间?我1分钟就足够了
PAT甲级真题1166
The difference between typescript let and VaR
c语言指针的概念
Leetcode 213: 打家劫舍 II
【CMake】CMake链接SQLite库
Common operations of JSP
Understanding of class
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
VMware virtual machine installation
Summary of Arduino serial functions related to print read
Topic | synchronous asynchronous
[plus de détails] dernière entrevue complète redis (50)
【无标题】
带你全流程,全方位的了解属于测试的软件事故
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
Hash table, generic
Comparison of advantages and disadvantages between most complete SQL and NoSQL