动态规划 - 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 ...
随机推荐
- 关于indexOf,charAt,subString的区别
@Test public void indexOf() { // 注意:在Unicode表中A-Z的十进制对应:65-90 // a-z的进制对应:97-122 // 0-9的十进制对应:48-57 ...
- 如何解决串session:
在IE快捷方式上点击鼠标右键>属性>快捷方式>目标:"C:\Program Files\Internet Explorer\iexplore.exe" -nome ...
- oracle中的insert all into,在mysql中的写法
oracle中的insert all into表示插入多条数据,mysql中可以采用: INSERT INTO表名(字段1,字段2..) values <foreach collection=& ...
- 2017-12-18python全栈9期第三天第一节之昨天内容回顾与作业讲解用户三次机会再试试
#!/user/bin/python# -*- coding:utf-8 -*-username = "zd"password = "123"i = 3whil ...
- nginx的下载、编译安装和启动
一.nginx简介 nginx(“engine x”)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器.nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- Java中的Dom4j
上一篇讲了Java中如何操作XML,现在介绍一个更厉害的方法,Dom4j ,百度查一下就知道,这个更强,更快,更简单. 自己下载jar包导入工具,下面来讲一个例子,我事先准备了一个XML文件,如下: ...
- Tomcat虚拟路径访问本地图片失败的问题
开发过程中,把图片放在本地一个磁盘的路径下.网上搜了下,可以使用tomcat虚拟路径访问本地图片. 这样就不用把图片放在整个项目的webapp目录下了. 1.找到tomcat的server.xml文件 ...
- Gym 100820C(级别排序 **)
题意是说有一些人参加了不同级别的班,级别有 lower,middle,upper 三种,级别可以组合,出现比如 lower upper,middle upper 这种级别,级别的比较是从右往左,如果在 ...
- 外部程序调用Django模块的解决办法
Question django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not ...
- javascript获取值
<div id='name'>张三</div> $('#name').val() $(name).val() 以上两个都可以得到值,第一种用的比较多.