当前位置:网站首页>198. 打家劫舍-动态规划
198. 打家劫舍-动态规划
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
示例 1:
输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 2:
输入:[2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
二、解题
动态规划
dp[i]表示下标为以i为下标时能偷到的最大值。
dp[i]=max(dp[i−2]+nums[i],dp[i−1])
class Solution {
public int rob(int[] nums) {
//解题思路比较简单 根据公式来算
//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://blog.csdn.net/hequnwang10/article/details/124349870
边栏推荐
- 1-5 nodejs commonjs specification
- Further study of data visualization
- [markdown notes]
- For the space occupation of the software, please refer to the installation directory
- 1217_使用SCons生成目标文件
- Node template engine (EJS, art template)
- Basic case of Baidu map
- Future 用法详解
- RPC核心概念理解
- How does matlab draw the curve of known formula and how does excel draw the function curve image?
猜你喜欢

C# Task. Delay and thread The difference between sleep

. net type transfer

基于51单片机红外无线通讯仿真

SiteServer CMS5. 0 Usage Summary
![[difference between Oracle and MySQL]](/img/90/6d030a35692fa27f1a7c63985af06f.png)
[difference between Oracle and MySQL]
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

ClickHouse-表引擎
![Using quartz under. Net core - [1] quick start](/img/80/b99417e88d544ca6e3da4c0c1625ce.png)
Using quartz under. Net core - [1] quick start

. net cross platform principle (Part I)
![Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger](/img/4e/2161fc448f4af71d9b73b7de64a17f.png)
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
随机推荐
[difference between Oracle and MySQL]
JS failed to change all variables and changed to the return method. Finally, the problem was solved
Further study of data visualization
Clickhouse table engine
Manually implement simple promise and its basic functions
1217_使用SCons生成目标文件
ASP. Net core configuration options (Part 1)
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
[simple understanding of database]
Preliminary understanding of promse
freeCodeCamp----prob_ Calculator exercise
Baidu Map Case - Zoom component, map scale component
Use of five routing guards
PC电脑使用无线网卡连接上手机热点,为什么不能上网
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
Promise (I)
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
Advantages and disadvantages of several note taking software
JVM类加载机制