动态规划 - 213. House Robber II
URL: https://leetcode.com/problems/house-robber-ii/
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.
解决方案:
Since it is a cyclied array, to make our dynamic programming alogrithm works, we need to break the cyclied array into two single-way arrays.
2-The priciple here is that either a particular house i-th can be robbed or not robbed at all.
For example 1 -> 3 -> 6 -> 5, we can break at the first position, so that the problem is divided to two parts,which are 1 -> 3 -> 6 (we choose to rob the first element, but not the last one.) and 3->6->5. Finally, we can reuse the House Robber I to solve these two sub-problems and pick out the optimal solution.
3-Here some of us may have the confusion that: we can rob house 1, it does not mean that we must rob house 1, whether we rob house 1 is depending on its next value. The correct complement of this statement is that: we should not(must not) rob house 1 ( and similarly, whether to rob the last house depends on the whole broken array.)
class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0) return 0;
if(nums.length == 1) return nums[0];
int len = nums.length;
int[] amount = new int[len];
//The first case: we can rob house 1, definitely, we cannot rob the last one
amount[0] = nums[0];
// whether we rob house 1 is depending the relative value
amount[1] = Math.max(nums[0],nums[1]);
for(int idx =2; idx < len-1; idx ++){
amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
}
// the last element is zero. amount[len-1] by default is zero
int totalSumCaseOne = amount[len-2];
// The second case: we don't rob house 1 at all.
amount = new int[len];
amount[0] = 0;
amount[1] = nums[1];
for(int idx = 2 ;idx< len; idx++){
amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
}
int totalSumCaseTwo = amount[len-1];
//select the largest one
return Math.max(totalSumCaseOne,totalSumCaseTwo);
}
}
动态规划 - 213. House Robber II的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- 213. House Robber II(动态规划)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 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 ...
- 213. House Robber II
题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
随机推荐
- plsql界面/command界面
存储过程执行CALL PRO_DELETE_OND_FOR_ORDERNO('120000000208'); --在PLSQL的SQL窗口执行 EXEC PRO_DELETE_OND_FOR_O ...
- 写给IT技术爱好者的一封信
写给IT技术爱好者的一封信>当前运维素质的分析<... ---------------------- 虽相貌平平,但勤学苦练,亦收获颇丰!如果你决定要成为一名IT从业者,你需要承受以下的东 ...
- mybatis多数据源报错
2018-12-06 16:58:35,709 [ main ] - [ INFO ] [ org.springframework.core.KotlinDetector : 57 ] - Kotli ...
- MySQL 8.0.14 新的密码认证方式和客户端链接
MySQL 8.0.14 新的密码认证方式和客户端链接 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MySQL8.0在密码认证方式发生了改变,这也是有点小伙伴在MySQL创建 ...
- Dubbo服务端结合maven打jar包
<build> <finalName>odao-weixin-user</finalName> <resources> ...
- [JUC-2]AbstractQueuedSynchronizer源码分析
AbstactQueuedSynchronizer的基本数据结构 AbstractQueuedSynchronizer的基本数据结构为Node,关于Node,JDK作者写了详细的注释,这里我大致总结几 ...
- 【由浅入深理解java集合】(三)——集合 List
第一篇文章中介绍了List集合的一些通用知识.本篇文章将集中介绍List集合相比Collection接口增加的一些重要功能以及List集合的两个重要子类ArrayList及LinkedList. 一. ...
- C# RestoreDirectory
OpenFileDialog与SaveFileDialog都有RestoreDirectory属性,这个属性默认是false,打开一 个文件后,那么系统默认目录就会指向刚才打开的文件.如果设为true ...
- TCP回射服务器修订版(ubuntu 18.04)
一.需求 把https://www.cnblogs.com/soldierback/p/10673345.html中的TCP回射服务器程序重写成使用select来处理任意个客户的单进程 程序,而不是为 ...
- jQuery two way bindings(双向数据绑定插件)
jQuery two way bindings https://github.com/petersirka/jquery.bindings 这是一个简单的jQuery双向绑定库. 此插件将HTML元素 ...