动态规划 - 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 ...
随机推荐
- bzoj1003 最短路+dp
遇到小范围数据的题目就容易被限制了思维,我单知道数据小可以跑很多遍最短路,但我没想到暴力跑N ^ 2的最短路也能过 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输 ...
- MySQL内存占用计算
##MySQL 最大可使用内存( M ): SELECT ( @@key_buffer_size + @@innodb_buffer_pool_size + @@query_cache_size + ...
- Java Web之表单重复提交问题
上篇文章讲了验证码的制作,提及到了一个问题,就是表单重复提交的问题,可能在上次那个验证码中感觉不是那么的重要 现在我新写一个例子,转钱.通过这个例子你就知道表单重复提交有多恐怖了. 先来简单的介绍一下 ...
- HDU - 5071 Chat(模拟)
原题链接 题意:有各种操作,模拟这个程序并输出每次操作的信息 分析:恶心模拟题...用个map记录一下各个等级女孩的谈话数,同时也便于查找权值为u的在不在队列里.因为n很小,其他就暴力模拟了. #in ...
- HDU 1041(01展开 大数)
题意是将 1 展开成 01 ,将 0 展开成 10 ,问这样展开 n 次后序列中有多少对 0. 手写发现:0,1,1,3,5,11 ... 即 a[ i ] = a[ i -1 ] + a[ i - ...
- STL优先队列
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> usin ...
- linux_网易云音乐安装
使用命令安装一些基本包$ sudo apt install devscripts equivs git
- oldboy s21day02
1.猜数字,设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了:如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环.while 1: num ...
- hashMap源码解析(五)
---恢复内容开始--- 首先抛出一个问题: 为什么hashMap一般使用String作为key? 这是我学习前辈们的博文时看到的一个问题,觉着很有意思,所以记录下来. 原因1: 我当时的第一反应是: ...
- MyList 泛型委托
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...