[LeetCode]House Robber II (二次dp)
213. House Robber II
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
这个题目是基于House Robber I 的,所以做这题之前要先知道HouseRobber I的解法。
House Robber I 的传送门:
如果已经ac了第一题,那么这题的意思就是把屋子都改成环状。
在第一题中,dp状态转移方程 max[ i ] = Math.max( max[ i - 1 ], nums[ i - 1 ] + max[ i - 2 ] ) 已经做出来了。那么第二题就很好做了。
第二题中,我的做法就是要再进行一次dp,并且需要记录下选择屋子的首尾,记为start和last.
在记录start的时候,要注意start的状态 : 1.当前面的屋子已经被选择。2.当前面的屋子没有被选择。
所以这里的dp转移方程总结为:
if( max[ i - 1 ] > nums[ i - 1 ] + max[ i - 2 ] ) {
max[ i ] = max[ i - 1 ];
start[ i ] = start[ i - 1 ];
} else {
max[ i ] = nums[ i - 1 ] + max[ i - 2 ];
last = i;
if( max[ i - 1 ] == max[ i - 2 ] ) {
start[ i ] = start[ i - 2 ];
} else {
start[ i ] = start[ i - 2 ] == 0 ? 2 : start[ i - 2 ];
}
}
最后判断一下,如果是选择最后一个的时候报警了,进行判断是要选择(1,n)还是(0,n-1)的最大价值
总的思想就是进行两次DP,(1,n)和(0,n-1)分别Dp
Ps:暂时没有想到更好更加简洁的方法。不过我觉得是有的,只是本人愚笨没想到
public class Solution { public int rob( int[] nums ) { int[] max = new int[ nums.length + 1 ];
int[] start = new int[ nums.length + 1 ];
max[ 0 ] = 0;
start[ 0 ] = 0;
if( nums == null || nums.length == 0 )
return 0;
max[ 1 ] = nums[ 0 ];
start[ 1 ] = 1;
int last = 1;
for( int i = 2; i <= nums.length; i++ ) {
if( max[ i - 1 ] > nums[ i - 1 ] + max[ i - 2 ] ) {
max[ i ] = max[ i - 1 ];
start[ i ] = start[ i - 1 ];
} else {
max[ i ] = nums[ i - 1 ] + max[ i - 2 ];
last = i;
if( max[ i - 1 ] == max[ i - 2 ] ) {
start[ i ] = start[ i - 2 ];
} else {
start[ i ] = start[ i - 2 ] == 0 ? 2 : start[ i - 2 ];
}
}
}
if( ( last + 1 ) % nums.length == start[ nums.length ] ) {
int[] tail = new int[nums.length-1];
System.arraycopy( nums, 1, tail, 0, nums.length-1 );
int preMax = rob2( tail );
max[ nums.length ] = Math.max( ( max[ nums.length ] - max[ 1 ] ), max[ nums.length - 1 ] );
max[ nums.length ] = Math.max( max[ nums.length ], preMax );
}
return max[ nums.length ];
} public int rob2( int[] nums ) { int[] max = new int[ nums.length + 1 ];
max[ 0 ] = 0;
if( nums == null || nums.length == 0 )
return 0;
max[ 1 ] = nums[ 0 ];
for( int i = 2; i <= nums.length; i++ ) {
max[ i ] = Math.max( max[ i - 1 ], nums[ i - 1 ] + max[ i - 2 ] );
}
return max[ nums.length ];
} public static void main( String[] args ) {
Solution s = new Solution();
int[] nums = new int[] { 2, 2, 4, 3, 2, 5 };
System.out.println( s.rob( nums ) );
} }
[LeetCode]House Robber II (二次dp)的更多相关文章
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- Leetcode House Robber II
本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Paint House II 粉刷房子之二
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LeetCode之“动态规划”:House Robber && House Robber II
House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...
- 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 You are a professional robber planning to rob houses along a street. Each house has ...
随机推荐
- iOS 登陆之界面设置
1.界面构成 1.1. 效果图 1.2. 元素 背景图 用户名的输入框 密码的输入框 登陆按钮 忘记密码 用户注册 第三方登陆 两个分割线
- [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds...
INSERT INTO `ftms_active_dealer`(dealer_code,dealer_name,active_id,dealer_state)VALUES('415A1','贺磊'1 ...
- HTML5 新元素、HTML5 Canvas
HTML5 新元素 自1999年以后HTML 4.01 已经改变了很多,今天,在HTML 4.01中的几个已经被废弃,这些元素在HTML5中已经被删除或重新定义. 为了更好地处理今天的互联网应用,HT ...
- Weblogic常见故障常:JDBC Connection Pools
http://blog.csdn.net/woshixuye/article/details/24122579 有些时候是数据库连接池出现了问题,测试的时候显示没有连接池了,重启WebLogic都不行 ...
- 在Eclipse中提交SVN项目的时候注意提交项目信息
提交项目的时候,注意提交.classpath,.project和.settings文件夹: 这些是项目的信息,别人下载的时候才能正确显示为Eclipse项目:
- 微信小程序教程(第三篇)
小程序的架构及实现机制,信道服务及会话管理 小程序架构及实现机制 小程序并不是 H5 应用,而是更偏向于传统的 CS 架构,它是基于数据驱动的模式,一切皆组件(视图组件).所以建议在开发小程序时不要以 ...
- Unity随手记
过年11天假期,带娃带了7天,吃吃喝喝.也看了点书,<射雕英雄传>(书)看了一半,还有就是在看<unity官方案例精讲>这本. 随手记一些自觉有价值或者有意思的点. 1. 对脚 ...
- TPS及计算方法
个事务,TPS为6 / 60s = 0.10 TPS.同时我们会知道事务的响应时间(或节拍),以此例,60秒完成6个事务也同时代表每个事务的响应时间或节拍为10秒. 利特尔法则 (Little' ...
- fir.im Weekly - 可能是 2017 最好的 Swift 学习资源
春节假期刚结束,一大批新鲜干货就来了.@故胤道长 分享了一份开源 Swift30 Projects ,内含 30 个小App,更新至 Swift 3.0,目前更迭的这个版本更注重代码规范和架构设计,且 ...
- Array方法归类总结
数组的转换方法 valueOf()方法,数组调用该方法后返回的还是原来的数组. toString()方法,数组调用该方法后会调用每一项的toStirng()方法,之后将每一项拼接成一个以逗号分割的字符 ...