当前位置:网站首页>LeetCode 198. Looting & dynamic planning
LeetCode 198. Looting & dynamic planning
2022-06-25 18:26:00 【7rulyL1ar】
Subject requirements
Link to the original title :198. raid homes and plunder houses
The questions are as follows :
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 .
Examples are as follows :
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 .
solution : Dynamic programming
Ideas
Observation topic requirements , Easy to know n A house , You can choose to steal or not
- If you steal n A house , Then according to the requirements of the topic, you can't steal the n-1 A house , The final total amount of theft is the former n-2 The maximum amount of a house is the same as that of n The sum of the amount of each house .
- If you don't steal n A house , Then the total amount of theft is the former n-1 The maximum amount of a house .
A large problem depends on the solution of its subproblem , It is obvious that dynamic programming can be used to solve .
For the first n The decision to steal a house or not , What needs to be compared is the former n-1 The maximum amount of a room and the previous n-2 The maximum amount of a house + The first n Which house has the largest amount .
Use dp[i] Before presentation i The maximum amount of money stolen from a house , We can get the following equation of state transition :
dp[i] = max(dp[i - 1, dp[i - 2] + nums[i])
According to the habitual thinking of dynamic planning , You might think of using a length equal to nums[] Array of dp[] Array for state representation , The spatial complexity of doing this is O(n),n Is the length of the original array , But by looking at the problem, we can find that , Each bottom-up traversal actually uses only dp[i - 2] and dp[i - 1], The rest of the space will no longer be used , So this can be optimized , Only two fixed extra spaces can be used to meet the demand , The space complexity is optimized to O(1) Level .
complete AC Code
class Solution {
public int rob(int[] nums) {
// Boundary condition judgment
if(nums.length == 1)return nums[0];
// dp[0] Forever means nums[ Current house - 2] The optimal solution of time
// dp[1] Forever means nums[ Current house - 1] The optimal solution of time
int[] dp = new int[2];
// initialization
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for(int i = 2; i < nums.length; i++){
int temp = dp[1];
dp[1] = Math.max(dp[0] + nums[i], dp[1]);
dp[0] = temp;
}
return dp[1];
}
}
Complexity analysis
Time complexity :O(n),n by nums The length of the array .
Spatial complexity :O(1), Just using extra variables at a fixed constant level .
边栏推荐
- 跨境电商亚马逊、eBay、Shopee、Lazada、速卖通、沃尔玛、阿里国际等平台,怎样进行自养号测评更安全?
- 【深入理解TcaplusDB技术】Tmonitor系统升级
- JSP页面运行却显示源码
- . How to exit net worker service gracefully
- 【深入理解TcaplusDB技术】TcaplusDB业务数据备份
- What is an operator?
- 【深入理解TcaplusDB技术】Tmonitor后台一键安装
- How can the self-supporting number evaluation be safer for cross-border e-commerce platforms such as Amazon, eBay, shopee, lazada, express, Wal Mart and Alibaba international?
- There is a repeating element iii[pruning with ordered ordering]
- .NET Worker Service 如何优雅退出
猜你喜欢
![[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc

Dell R530内置热备盘状态变化说明

【深入理解TcaplusDB技术】Tmonitor后台一键安装

Use pagoda to set up mqtt server

揭秘GES超大规模图计算引擎HyG:图切分

Sword finger offer double pointer

Handling method of qstring containing "\u0000" in QT

視覺SLAM十四講 第9講 卡爾曼濾波

Class 02 loader subsystem

One article solves all search backtracking problems of Jianzhi offer
随机推荐
RMAN备份数据库_跳过脱机,只读和不可访问的文件
篇6:CLion:Toolchains are not configured Configure Disable profile
Batch uploading of local jar packages to nexus private server
[in depth understanding of tcapulusdb technology] tcapulusdb construction data
[deeply understand tcapulusdb technology] table management of document acceptance
Handling method of qstring containing "\u0000" in QT
【深入理解TcaplusDB技术】单据受理之表管理
C generic class case
中断操作:AbortController学习笔记
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc
General message publishing and subscription in observer mode
Is it convenient to open a stock account? Is online account opening safe?
05 virtual machine stack
[how do I refresh the shuttle page after jumping back?]
安装spark + 用命令运行scala相关项目 + crontab定时执行
1. Understanding of norm
Redis command string
LeetCode力扣(剑指offer 26-30)26. 树的子结构 27. 二叉树的镜像 28. 对称的二叉树 29. 顺时针打印矩阵 30. 包含min函数的栈
Network security algorithm
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor