当前位置:网站首页>198. house raiding
198. house raiding
2022-06-24 09:20:00 【Zzu dish】
198. raid homes and plunder houses
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 .
reflection
Definition dp The meaning of array and its subscript
dp[i]: Before presentation i The maximum amount that can be stolen in days is dp[i]
initialization dp Array
initialization dp[0] and dp[1]
State transition equation
dp[i] = Max{dp[i-1], dp[i-2]+nums[i] }
class Solution {
public int rob(int[] nums) {
if(nums.length==1) return nums[0];
//step 1 establish dp Array
int[] dp=new int[nums.length];
// step 2 initialization dp Array
dp[0] = nums[0];
if(nums[1]>nums[0])
dp[1] = nums[1];
else dp[1] = dp[0];
if(dp.length==2) return dp[1];
// step 3 perfect dp Array
for (int i=2;i<dp.length;i++){
dp[i] = Math.max(dp[i-1],dp[i-2]+nums[i]);
}
return dp[dp.length-1];
}
}
边栏推荐
- 110. balanced binary tree recursive method
- leetcode--字符串
- Go 语言项目开发实战目录
- Weekly recommended short video: is the ultimate form of computing "meta universe"?
- 4274. suffix expression
- Project deployment related
- Remote connection of raspberry pie without display by VNC viewer
- Mba-day25 best value problem - application problem
- Squid代理服务器应用
- 深入了解 border
猜你喜欢

Kaformer personal notes

YOLOX backbone——CSPDarknet的实现

uniapp 开发多端项目如何配置环境变量以及区分环境打包
![[Niuke] convert string to integer](/img/56/3e491b3d0eea0d89a04d0b871501d7.png)
[Niuke] convert string to integer

L01_一条SQL查询语句是如何执行的?
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)

Solution: the word of jmeter5.5 on the win11 lower interface is very small

Xiaobai needs to learn MySQL - incremental statistical SQL
![[e325: attention] VIM editing error](/img/58/1207dec27b3df7dde19d03e9195a53.png)
[e325: attention] VIM editing error

Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
随机推荐
Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
Implementation process of tcpdump packet capturing
Every (), map (), forearch () methods. There are objects in the array
What do you mean by waiting for insurance records? Where should I go for filing?
嵌入式 | 硬件转软件的几条建议
Lu Qi: I am most optimistic about these four major technology trends
学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
Spark - the number of leftouterjoin results is inconsistent with that of the left table
深入解析 Apache BookKeeper 系列:第三篇——读取原理
Huawei Router: IPSec Technology
520. detect capital letters
Essay - Reflection
Target detection series fast r-cnn
Squid代理服务器应用
Redis实现全局唯一ID
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
[quantitative investment] discrete Fourier transform to calculate array period
Support vector machine (SVC, nusvc, linearsvc)
Depens:*** but it is not going to be installed
Zero foundation self-study SQL course | sub query