当前位置:网站首页>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];
}
}
边栏推荐
- 金仓KFS replicator安装(Oracle-KES)
- Scheme of alcohol concentration tester based on single chip microcomputer
- 学习太极创客 — ESP8226 (十三)OTA
- Target detection series fast r-cnn
- Sword finger offer 55 - I. depth DFS method of binary tree
- 普通人没有学历,自学编程可以月入过万吗?
- Zero foundation self-study SQL course | having clause
- Data midrange: detailed explanation of the technical stack of data acquisition and extraction
- Microblog writing - flow chart - sequence chart - Gantt chart - Mermaid flow chart - good results
- 4275. Dijkstra sequence
猜你喜欢

Zero foundation self-study SQL course | sub query
![[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization](/img/a3/9a50e83ece43904a3a622dcdb05b3c.png)
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization

cookie加密 4 rpc方法确定cookie加密
Depens:*** but it is not going to be installed

Numpy numpy中的np.c_和np.r_详解

活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中

The border problem after the focus of input

2022.06.23 (traversal of lc_144,94145\

Redis implements a globally unique ID

华为路由器:ipsec技术
随机推荐
Cmake命令之target_compile_options
【LeetCode】387. First unique character in string
Leetcode -- wrong set
从618看京东即时零售的野心
Lu Qi: I am most optimistic about these four major technology trends
MySQL data (Linux Environment) scheduled backup
[bug] @jsonformat has a problem that the date is less than one day when it is used
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
520. detect capital letters
Zero foundation self-study SQL course | sub query
Rpiplay implementation of raspberry pie airplay projector
Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
[noi Simulation Competition] send (tree DP)
PM2 deploy nuxt3 JS project
【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
Data midrange: detailed explanation of the technical stack of data acquisition and extraction
How to import MDF and LDF files into MySQL workbench
Epidemic situation, unemployment, 2022, we shouted to lie down!
Numpy numpy中的np.c_和np.r_详解