当前位置:网站首页>【Brush the title】Family robbery
【Brush the title】Family robbery
2022-08-02 01:36:00 【m0_60631323】
一、题目
OJ链接
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 .
给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额.
示例:
输入:nums = [2,3,2]
输出:3
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的.
二、题解
2.1动态规划
Essentially, what is sought is the maximum cumulative sum of subsequences in the case that adjacent numbers cannot be taken
But this question has a special limitation:
That is, the first number and the last number in the array are considered adjacent,That is, we cannot choose the first number and the last number at the same time,那么我们可以这样做,分两种情况
1)从0~n-2上做选择
2)从1~n-1上做选择
返回两种情况的较大值
源码:
️
public int rob (int[] nums) {
if(nums.length==1) {
return nums[0];
}
if(nums.length==2) {
return Math.max(nums[0],nums[1]);
}
int N=nums.length;
int[] dp1=new int[N];
dp1[0]=nums[0];
dp1[1]=Math.max(nums[0],nums[1]);
for(int i = 2; i <N-1; i++){
int p1=dp1[i-1];
int p2=dp1[i-2]+nums[i];
dp1[i]=Math.max(p1,p2);
}
int[] dp2=new int[N];
dp2[1]=nums[1];
dp2[2]=nums[2];
for (int i = 3; i <N ; i++) {
int p1=dp2[i-1];
int p2=dp2[i-2]+nums[i];
dp2[i]=Math.max(p1,p2);
}
return Math.max(dp1[N-2],dp2[N-1]);
}
This code can still be optimized,因为dp[i]只依赖dp[i-1]和dp[i-2],So we can use several variable scrolling insteaddp数组
优化后:
️
public int rob(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
if (nums.length == 2) {
return Math.max(nums[0], nums[1]);
}
int N= nums.length;
int pre1=nums[0];
int pre2=Math.max(nums[0],nums[1]);
for (int i = 2; i < N-1; i++) {
int tmp=Math.max(pre1+nums[i],pre2);
pre1=pre2;
pre2=tmp;
}
int ans1=pre2;
pre1=nums[1];
pre2=Math.max(nums[1],nums[2]);
for (int i = 3; i < N; i++) {
int tmp=Math.max(pre1+nums[i],pre2);
pre1=pre2;
pre2=tmp;
}
int ans2=pre2;
return Math.max(ans1,ans2);
}
边栏推荐
- 滴滴秋招提前批正式开始,现在投递免笔试
- GateWay实现负载均衡
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- 管理基础知识9
- H5画布 canvas(一)canvas简介、绘制圆形矩形、案例饼状图绘制
- JDBC PreparedStatement 的命名参数实现
- 浅谈国产ERP的“横纵竖”三向发展态势
- When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
- C语言实验十 函数(二)
- FlinkSQL CDC实现同步oracle数据到mysql
猜你喜欢
安全(2)
Image fusion based on weighted 】 and pyramid image fusion with matlab code
datax与datax-web安装部署
Reflex WMS中阶系列6:对一个装货重复run pick会有什么后果?
H5画布 canvas(一)canvas简介、绘制圆形矩形、案例饼状图绘制
go版本升级
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
管理基础知识21
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
flex布局中使用flex-wrap实现换行
随机推荐
创新项目实战之智能跟随机器人原理与代码实现
canal realizes mysql data synchronization
【软件工程之美 - 专栏笔记】34 | 账号密码泄露成灾,应该怎样预防?
GO开发环境配置
去经营企业吧
flv.js解析与使用
Rust P2P网络应用实战-1 P2P网络核心概念及Ping程序
Debian侵犯Rust商标,妥协改名还是会得到豁免?
Day11 Shell scripting basics
H5画布 canvas(一)canvas简介、绘制圆形矩形、案例饼状图绘制
Flex layout in detail
管理基础知识11
go版本升级
信息收集之目录扫描-dirbuster
Use flex-wrap to wrap lines in flex layout
好的期货公司开户让人省心省钱
iframe使用
Markdown (CSDN) MD编辑器(四)- 漂亮表格(表格背景色、跨行、跨列)
Mapped Statements collection does not contain value for的解决方法
管理基础知识15