[LeetCode] 213. House Robber II 打家劫舍之二

时间:2021-08-26 23:12:41

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
  because they are adjacent houses.

Example 2:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
  Total amount you can rob = 1 + 3 = 4.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

这道题是之前那道 House Robber 的拓展,现在房子排成了一个圆圈,则如果抢了第一家,就不能抢最后一家,因为首尾相连了,所以第一家和最后一家只能抢其中的一家,或者都不抢,那这里变通一下,如果把第一家和最后一家分别去掉,各算一遍能抢的最大值,然后比较两个值取其中较大的一个即为所求。那只需参考之前的 House Robber 中的解题方法,然后调用两边取较大值,代码如下:

解法一:

class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
return max(rob(nums, , nums.size() - ), rob(nums, , nums.size()));
}
int rob(vector<int> &nums, int left, int right) {
if (right - left <= ) return nums[left];
vector<int> dp(right, );
dp[left] = nums[left];
dp[left + ] = max(nums[left], nums[left + ]);
for (int i = left + ; i < right; ++i) {
dp[i] = max(nums[i] + dp[i - ], dp[i - ]);
}
return dp.back();
}
};

当然,我们也可以使用两个变量来代替整个 DP 数组,讲解与之前的帖子 House Robber 相同,分别维护两个变量 robEven 和 robOdd,顾名思义,robEven 就是要抢偶数位置的房子,robOdd 就是要抢奇数位置的房子。所以在遍历房子数组时,如果是偶数位置,那么 robEven 就要加上当前数字,然后和 robOdd 比较,取较大的来更新 robEven。这里就看出来了,robEven 组成的值并不是只由偶数位置的数字,只是当前要抢偶数位置而已。同理,当奇数位置时,robOdd 加上当前数字和 robEven 比较,取较大值来更新 robOdd,这种按奇偶分别来更新的方法,可以保证组成最大和的数字不相邻,最后别忘了在 robEven 和 robOdd 种取较大值返回,代码如下:

解法二:

class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
return max(rob(nums, , nums.size() - ), rob(nums, , nums.size()));
}
int rob(vector<int> &nums, int left, int right) {
int robEven = , robOdd = ;
for (int i = left; i < right; ++i) {
if (i % == ) {
robEven = max(robEven + nums[i], robOdd);
} else {
robOdd = max(robEven, robOdd + nums[i]);
}
}
return max(robEven, robOdd);
}
};

另一种更为简洁的写法,讲解与之前的帖子 House Robber 相同,我们使用两个变量 rob 和 notRob,其中 rob 表示抢当前的房子,notRob 表示不抢当前的房子,那么在遍历的过程中,先用两个变量 preRob 和 preNotRob 来分别记录更新之前的值,由于 rob 是要抢当前的房子,那么前一个房子一定不能抢,所以使用 preNotRob 加上当前的数字赋给 rob,然后 notRob 表示不能抢当前的房子,那么之前的房子就可以抢也可以不抢,所以将 preRob 和 preNotRob 中的较大值赋给 notRob,参见代码如下:

解法三:

class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
return max(rob(nums, , nums.size() - ), rob(nums, , nums.size()));
}
int rob(vector<int> &nums, int left, int right) {
int rob = , notRob = ;
for (int i = left; i < right; ++i) {
int preRob = rob, preNotRob = notRob;
rob = preNotRob + nums[i];
notRob = max(preRob, preNotRob);
}
return max(rob, notRob);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/213

类似题目:

House Robber

House Robber III

Paint House

Paint Fence

Non-negative Integers without Consecutive Ones

Coin Path

参考资料:

https://leetcode.com/problems/house-robber-ii/

https://leetcode.com/problems/house-robber-ii/discuss/59929/Java-clean-short-solution-DP

https://leetcode.com/problems/house-robber-ii/discuss/59934/Simple-AC-solution-in-Java-in-O(n)-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 213. House Robber II 打家劫舍之二的更多相关文章

  1. &lbrack;LeetCode&rsqb; 213&period; House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  2. &lbrack;LintCode&rsqb; House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  3. &lbrack;LeetCode&rsqb; House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  4. 213 House Robber II 打家劫舍 II

    注意事项: 这是 打家劫舍 的延伸.在上次盗窃完一条街道之后,窃贼又转到了一个新的地方,这样他就不会引起太多注意.这一次,这个地方的所有房屋都围成一圈.这意味着第一个房子是最后一个是紧挨着的.同时,这 ...

  5. Java for LeetCode 213 House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  6. LeetCode 213&period; House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  7. &lbrack;leetcode&rsqb; &num;213 House Robber II Medium &lpar;medium&rpar;

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

  8. &lbrack;LeetCode&rsqb; 337&period; House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  9. leetcode 198&period; House Robber 、 213&period; House Robber II 、337&period; House Robber III 、256&period; Paint House&lpar;lintcode 515&rpar; 、265&period; Paint House II&lpar;lintcode 516&rpar; 、276&period; Paint Fence&lpar;lintcode 514&rpar;

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

随机推荐

  1. 转: CentOS 安装 SVN1&period;8 客户端

     from: http://blog.csdn.net/clementad/article/details/46898091 CentOS 安装SVN客户端 标签: subversionrpmcent ...

  2. 单选按钮控件(Ridio Button)的使用

    VC学习笔记5:单选按钮控件(Ridio Button)的使用 一.对单选按钮进行分组: 每组的第一个单选按钮设置属性:Group,Tabstop,Auto;其余按钮设置属性Tabstop,Auto. ...

  3. Python中Cookie的处理(一)Cookie库

    Cookie用于服务器实现会话,用户登录及相关功能时进行状态管理.要在用户浏览器上安装cookie,HTTP服务器向HTTP响应添加类似以下内容的HTTP报头: Set-Cookie:session= ...

  4. JavaScript 三种创建对象的方法

    JavaScript中对象的创建有以下几种方式: (1)使用内置对象 (2)使用JSON符号 (3)自定义对象构造 一.使用内置对象 JavaScript可用的内置对象可分为两种: 1,JavaScr ...

  5. 关于windows10调试应用注册失败

    搜索了一些方法,都是win8系统的应用程序解决方案,修改应用程序的包名.也尝试修改了一些,但是失败,任然报错,以前在这个机器上面是能正常调试的,唯一的不同点是就是系统升级到10166了,于是去设置里面 ...

  6. 【原创】Mac上编译Hadoop1&period;0&period;3出现的一些问题

    create-native-configure: [exec] configure.ac:47: error: possibly undefined macro: AC_PROG_LIBTOOL [e ...

  7. QtWaitingSpinner

    https://github.com/snowwlex/QtWaitingSpinner

  8. 开源社群系统ThinkSNS&plus;安装部署演示视频!

    社群系统TS+一期版本发布之后,很多小伙伴们反馈安装部署有些困难,那么今天由我们的颜值与技术实力担当乔斌大佬通过录制视频的形式,给大家演示一下部署的整个过程,录制过程中有些杂音,请各位尽情谅解,后续我 ...

  9. gdb分析core文件

    转载自:http://blog.chinaunix.net/u2/83905/showart_2134570.html 在Unix系统下,应用程序崩溃,一般会产生core文件,如何根据core文件查找 ...

  10. 机器学习 支持向量机(SVM) 从理论到放弃,从代码到理解

    基本概念 支持向量机(support vector machines,SVM)是一种二分类模型,它的基本模型是定义在特征空间上的间隔最大的线性分类器.支持向量机还包括核技巧,这使它成为实质上的非线性分 ...