当前位置:网站首页>213.打家劫舍 II
213.打家劫舍 II
2022-06-25 22:02:00 【zzu菜】
打家劫舍 II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。
给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额。
示例 1:
输入:nums = [2,3,2]
输出:3
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
示例 2:
输入:nums = [1,2,3,1]
输出:4
解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 3:
输入:nums = [1,2,3]
输出:3
思考
本题只要利用198打家劫舍的方法获取前n-1天的最大盗窃数和后n-1天的最大盗窃数,取最大值就行了。
class Solution {
public int rob(int[] nums) {
if(nums.length==1) return nums[0];
if (nums.length==2) return Math.max(nums[0],nums[1]);
int a=robTools(Arrays.copyOfRange(nums,0,nums.length-1));
int b=robTools(Arrays.copyOfRange(nums,1,nums.length));
return Math.max(a,b);
}
public int robTools(int[] nums) {
if(nums.length==1) return nums[0];
//step 1 创建dp数组
int[] dp=new int[nums.length];
// step 2 初始化dp数组
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 完善dp数组
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];
}
}
边栏推荐
- 分享一个OSGeo4W64下载好的库,基于qgis3.10的
- String对象(常量)池
- Repoptimizer: it's actually repvgg2
- OBS-Studio-27.2.4-Full-Installer-x64. Exe Download
- Tree class query component
- What is Unified Extensible Firmware Interface (UEFI)?
- Pycharm student's qualification expires, prompting no suitable licenses associated with account solution
- Leetcode (435) - no overlapping interval
- 树状类查询组件
- Rk3568+ Hongmeng industrial control board industrial gateway video gateway solution
猜你喜欢

Beacon realizes asset management and indoor positioning based on 5.2 ultra-low power Bluetooth module efr32 (bg22ax)

Problem recording and thinking
![[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood](/img/36/8ad6034473382f66f315eb70440711.png)
[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood
Why is BeanUtils not recommended?

首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千

character string

【无标题】打开一个项目连接,无法正常显示时,ping一下ip

(serial port Lora module) centrida rf-al42uh private protocol test at instruction test communication process

Use of xinchida ble 5.0 Low Power Bluetooth module (at command serial port transparent transmission) rsbrs02abr

经典图像分割网络:Unet 支持libtorch部署推理【附代码】
随机推荐
To solve the incompatibility between VM and device/credential guard, an effective solution for the whole network
Reproduction of an implant found by Kaspersky that writes shellcode into evenlog
hiberate实体类CURD、事务操作汇总
[opencv450 samples] read the image path list and maintain the proportional display
QComboBox下拉菜单中有分隔符Separator时的样式设置
二进制、16进制、大端小端
Go language escape analysis complete record
对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现
B. Box Fitting-CodeCraft-21 and Codeforces Round #711 (Div. 2)
Hibernate entity class curd, transaction operation summary
[untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
Live800 online customer service system: do business across time and space, starting from each interaction
cookie、session、token
Solve 'tuple' object has no attribute 'lower‘
Problem recording and thinking
The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
Hbuilderx uses the gaude map to obtain the current location
Konva series tutorial 2: drawing graphics
Customize the qcombobox drop-down box, right align the display, and slide the drop-down list
C2. k-LCM (hard version)-Codeforces Round #708 (Div. 2)