当前位置:网站首页>剑指 Offer II 089. 房屋偷盗 / 剑指 Offer II 090. 环形房屋偷盗
剑指 Offer II 089. 房屋偷盗 / 剑指 Offer II 090. 环形房屋偷盗
2022-06-21 16:01:00 【彼淇梁】
剑指 Offer II 089. 房屋偷盗【中等题】
思路:【动态规划】
只要找出转移方程就不难,有两种写法,一种是dp数组,一种是滚动数组。
代码:【dp数组】
class Solution {
public int rob(int[] nums) {
int n = nums.length;
//排除特殊情况
if (n == 1){
return nums[0];
}
//定义dp数组 dp[i]表示 走到第i号房屋时,能够偷到的最高金额
int[] dp = new int[n];
//dp[0] 和 dp[1]赋初值
dp[0] = nums[0];
dp[1] = Math.max(nums[0],nums[1]);
for (int i = 2; i < n; i++) {
//动态转移方程
dp[i] = Math.max(dp[i-2]+nums[i],dp[i-1]);
}
//返回走到最后一个房屋时,能够偷到的最大金额
return dp[n-1];
}
}
代码:【滚动数组】
class Solution {
public int rob(int[] nums) {
int n = nums.length;
//排除特殊情况
if (n == 1){
return nums[0];
}
//定义滚动数对 a 和 b 表示走到某间房屋时,能够偷到的最大金额
//a 和 b 紧邻且a在b左侧 首先为 a 和 b 赋初值
int a = nums[0];
int b = Math.max(nums[0],nums[1]);
for (int i = 2; i < n; i++) {
//动态转移方程,c表示紧邻b房间在b右侧的房间能偷到的最大金额
int c = Math.max(a+nums[i],b);
a = b;
b = c;
}
//返回走到最后一个房屋时,能够偷到的最大金额
//由于滚动到最后一位时,b会更新为c,而c是局部变量,所以这里返回b即可
return b;
}
}
剑指 Offer II 090. 环形房屋偷盗【中等题】
思路:【动态规划】
与上题思路一致,但要把端点处分为两种情况考虑,由于题目限制,当你从第0家开始偷,就只能在第n-2家结束;从第1家开始偷,则可以在第n-1家结束。
代码:【dp数组】
class Solution {
public int rob(int[] nums) {
int n = nums.length;
//排除特殊情况
if (n == 1){
return nums[0];
}
if (n == 2){
return Math.max(nums[0],nums[1]);
}
//定义dp数组 dp[i]表示 走到第i号房屋时,能够偷到的最高金额
int[] dp = new int[n];
//dp[0] 和 dp[1]赋初值 两种情况 从第0间开始偷 则 必不能选第n-1间 从 第1间开始偷,则可以选择第n-1间
//1
dp[0] = nums[0];
dp[1] = Math.max(nums[0],nums[1]);
for (int i = 2; i < n; i++) {
//动态转移方程
dp[i] = Math.max(dp[i-2]+nums[i],dp[i-1]);
}
//第1种情况的最大偷盗金额,存入ans
int ans = dp[n-2];
//2
dp[1] = nums[1];
dp[2] = Math.max(nums[1],nums[2]);
for (int i = 3; i < n; i++) {
//动态转移方程
dp[i] = Math.max(dp[i-2]+nums[i],dp[i-1]);
}
//求出第2种情况的最大偷盗金额后更新ans
ans = Math.max(ans,dp[n-1]);
return ans;
}
}
代码:【滚动数组】
class Solution {
public int rob(int[] nums) {
int n = nums.length;
//排除特殊情况
if (n == 1){
return nums[0];
}
if (n == 2){
return Math.max(nums[0],nums[1]);
}
//定义滚动数对 a 和 b 表示走到某间房屋时,能够偷到的最大金额
//a 和 b 紧邻且a在b左侧 首先为 a 和 b 赋初值
int a = nums[0];
int b = Math.max(nums[0],nums[1]);
for (int i = 2; i < n; i++) {
//动态转移方程,c表示紧邻b房间在b右侧的房间能偷到的最大金额
int c = Math.max(a+nums[i],b);
a = b;
b = c;
}
//遍历结束时,a为第n-2位的最大偷盗金额 b 为第n-1位的最大偷盗金额
//第1种情况的最大偷盗金额在第n-2位,存入ans
int ans = a;
//2
a = nums[1];
b = Math.max(nums[1],nums[2]);
for (int i = 3; i < n; i++) {
//动态转移方程
int c = Math.max(a+nums[i],b);
a = b;
b = c;
}
//第2种情况的最大偷盗金额在第n-1位,更新ans
ans = Math.max(ans,b);
//返回两种最大偷盗金额的最大值
return ans;
}
}
边栏推荐
- [live broadcast preview] at 19:00 on June 24, hcsd live broadcast -- employment guide, which will take you through the interview points for the upcoming autumn recruitment and summer internship~~
- How to write test cases
- Alibaba cloud server + pagoda panel + no domain name deployment web project
- Unittest框架的测试日志
- [observation] Microsoft's "cloud + end" comprehensive innovation makes hybrid cloud simpler, more flexible and more secure
- Which futures company is better to open an account at present? Is the service charge low and the transaction safe?
- 使用 Guzzle 中间件进行优雅的请求重试
- Postman基本操作
- Esp8266 / esp32 obtenir la méthode NTP time via la Bibliothèque timelib
- 机器学习模型监控(Aporia)
猜你喜欢

Pytest--生成测试报告

Test log of unittest framework

ESP8266/ESP32 通過TimeLib庫獲取NTP時間方法

Unable to boot device in current state: started - unable to boot device in current state: booted

Cisco(35)——BGP入门实验

Yaml文件详解

Huawei cloud releases desktop ide codearts

如何编写测试用例

Yaml数据驱动演示

Wechat applet development tutorial - Introduction to text components
随机推荐
Qtcreator error reporting solution
【1108. IP 地址無效化】
Cloud native monitoring system - Nightingale's recent list of new functions to solve multiple production pain points
qtcreator报错解决
聚焦工业智能化场景,华为云招募合作伙伴,帮助解决转型难题
进击的程序员,如何提升研发效能?|直播预告
容器云是什么意思?与堡垒机有什么区别?
Previous installation records
Undefined functions or variables [explained in one article] (matlab)
Ares Ares I pledged LP mining crowdfunding model DAPP smart contract customization
阿里云服务器+宝塔面板+无域名部署web项目
Cisco(59)——Hub&Spoke MPLS
Google Play Academy 组队 PK 赛,正式开赛!
D improve translation
UDP和TCP的对比
[SQLite] solve unrecognized token:“‘“
互联网公司做单元测试吗?银行的需求有必要做单元测试吗?
Fidder工具使用笔记
The first atlas showing the development history of the database in China was officially released!
关于产品运营策略