当前位置:网站首页>198. House raiding
198. House raiding
2022-07-24 02:54:00 【Anji_ lh1029】
1、 Title Description

2、 Topic analysis
This topic is 【 Dynamic programming 】 Classic topics , Previous pair 【 The problem of buying stocks 】【 Home raiding 】 Special analysis has been carried out , Thematic analysis will summarize and compare topics of the same type , It is more comprehensive than this single topic . I suggest you read what I wrote before , Blog posts on such topics , The corresponding links are as follows :
Therefore, this kind of problem is mainly to find recursive equations . The recursive idea is as follows :

According to the above recursive logic , Use auxiliary space dp The code implementation is as follows :
class Solution {
public int rob(int[] nums) {
// The initial conditions
int n = nums.length;
if(n == 0) return 0;
if(n == 1) return nums[0];
// Dynamic programming , Auxiliary space dp
int[] dp = new int[n];
// To initialize
dp[0] = nums[0];
dp[1] = Math.max(nums[0],nums[1]);
for(int i = 2; i < n; i++){
// Dynamic programming transfer equation
dp[i] = Math.max(dp[i-2]+nums[i], dp[i-1]);
}
return dp[n-1];
}
}Complexity analysis
Time complexity :O(n), among n It's array length . Just traverse the array once .
Spatial complexity :O(n). Use auxiliary space dp Array , Record the temporary results of each element .
Space optimization based on the above , Because of the discovery , The numerical , Just follow 【 First two rooms 】 The amount of the house , Therefore, there is no need for special use dp Auxiliary space for recording , It can be changed into a variable , In this way, the space complexity is reduced from the original array O(n) Reduce to a finite number of variable elements , The space complexity is reduced to O(1).
here ,【 Optimize space complexity 】 Let's write it as follows :
class Solution {
public int rob(int[] nums) {
// The initial conditions
int n = nums.length;
if(n == 0) return 0;
if(n == 1) return nums[0];
int one = nums[0];
int second = Math.max(nums[0],nums[1]);
for(int i =2; i< n; i++){
// Rolling calculation
int temp = second;
second = Math.max(one+nums[i], second);
one = temp;
}
return second;
}
}Complexity analysis
Time complexity :O(n), among n It's array length . Just traverse the array once .
Spatial complexity :O(1). Use scrolling arrays , You can only store the maximum total amount of the first two houses , Instead of storing the results of the entire array , So the complexity of space is O(1).
边栏推荐
- Nodejs builds cloud native microservice applications based on dapr, a quick start guide from 0 to 1
- SSM family financial management personal financial management system accounting system
- [leetcode] sword finger offer 61. shunzi in playing cards
- SkyWalking分布式系统应用程序性能监控工具-上
- (六)装饰器扩展之[email protected]使用原理
- kettle
- Attack and defense world web practice area (view_source, get_post, robots)
- X Actual combat - Cloud Server
- JVM初始
- Dynamic programming-01 knapsack problem
猜你喜欢

Acwing 4498. pointer (DFS)

理解加载class到JVM的时机

Wonderful! The description of meituan Octo distributed service management system is too clear
![js傳參時傳入 string有數據;傳入 number時沒有數據;2[0]是對的!number類型數據可以取下標](/img/4e/3d0c25d9579b6d5c00473048dbbd83.png)
js傳參時傳入 string有數據;傳入 number時沒有數據;2[0]是對的!number類型數據可以取下標

Ugui source code analysis - Mask

Analyze the overall planning of steam and maker education classroom

Doodle Icons - 一组免费商用的涂鸦风格图标库,可爱轻快又独特

508. The subtree element with the most occurrences and the pure C implementation of hash table method

攻防世界WEB练习区(weak_auth、simple_php、xff_referer)

SkyWalking分布式系统应用程序性能监控工具-上
随机推荐
AcWing 4499. 画圆 (相似三角形)
TCP data transmission and performance
Unity TimeLine使用教程
The practical influence of educational robot on students' learning effect
Unscramble the category and application principle of robot vision
Example of producer consumer code implemented by the destructor framework without lock
Ugui source code analysis - iclippable
[leetcode] sword finger offer 61. shunzi in playing cards
Is Zhongcheng hospital really helping suppliers solve problems?
Force open web page
7月开发过程中遇到的问题总结
Go IO operation - file read
PMP first-hand data and information acquisition
AcWing 4498. 指针 (DFS)
Ugui source code analysis - imaskable
Mysql database, query
Mysql database, grouping function
C language exercises
LeetCode-栈和队列刷题
让生活充满幸福感