当前位置:网站首页>198. Looting - Dynamic Planning
198. Looting - Dynamic Planning
2022-04-23 17:33:00 【hequnwang10】
One 、 Title Description
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 .
Two 、 Problem solving
Dynamic programming
dp[i] Indicates that the subscript is i The maximum value that can be stolen when it is a subscript .
dp[i]=max(dp[i−2]+nums[i],dp[i−1])
class Solution {
public int rob(int[] nums) {
// The idea of solving problems is relatively simple According to the formula
//dp[i]=max(dp[i−2]+nums[i],dp[i−1])
int length = nums.length;
if(length == 0 || nums == null){
return 0;
}
if(length == 1){
return nums[0];
}
int[] dp = new int[length+1];
dp[1] = nums[0];
for(int i = 2;i<=length;i++){
dp[i] = Math.max(dp[i-2]+nums[i-1],dp[i-1]);
}
return dp[length];
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009140.html
边栏推荐
- 索引:手把手教你索引从零基础到精通使用
- 超分之TDAN
- Come out after a thousand calls
- 239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
- Clickhouse table engine
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
- PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
- .Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
- JS to find the character that appears three times in the string
- 为什么有些人说单片机简单,我学起来这么吃力?
猜你喜欢
Tdan over half
MySQL installation
Understanding of RPC core concepts
Perception of linear algebra 2
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
QT modification UI does not take effect
SiteServer CMS5. 0 Usage Summary
Qt 修改UI没有生效
48. 旋转图像
Matlab / Simulink simulation of double closed loop DC speed regulation system
随机推荐
ClickHouse-SQL 操作
Basic case of Baidu map
Websocket (basic)
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
C语言函数详解
[simple understanding of database]
Use of shell awk command
ASP. Net core dependency injection service life cycle
Promise (IV)
线性代数感悟之1
Halo 开源项目学习(二):实体类与数据表
402. 移掉 K 位数字-贪心
Further study of data visualization
Clickhouse SQL operation
How to manually implement the mechanism of triggering garbage collection in node
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
Double pointer advanced -- leetcode title -- container with the most water
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
Use of shell sed command