lintcode:打劫房屋II
题目
在上次打劫完一条街道之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子围成了一个圈,这就意味着第一间房子和最后一间房子是挨着的。每个房子都存放着特定金额的钱。你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警。
给定一个非负整数列表,表示每个房子中存放的钱, 算一算,如果今晚去打劫,你最多可以得到多少钱 在不触动报警装置的情况下。
样例
给出nums = [3,6,4]
, 返回 6
, 你不能打劫3
和4
所在的房间,因为它们围成一个圈,是相邻的.
解题
根据题目,第0个房间和第n-1个房间只能打劫其中一个
打劫范围只能是:0-n-2 或者1-n-1
所以根据打劫房间I的程序,修改为根据范围进行打劫,再求这个两个打劫的最大值。
下面程序定义的dp数组,dp[i]表示打劫当前房间能够获得最大值
最后的结果要返回最后两个值得最大值
public class Solution {
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public int houseRobber2(int[] nums) {
// write your code here
if(nums==null || nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
if(nums.length == 2)
return Math.max(nums[0],nums[1]);
if(nums.length == 3)
return Math.max(Math.max(nums[0],nums[1]),nums[2]);
int len = nums.length;
int res1 = houseRobber(nums,0,len-2);
int res2 = houseRobber(nums,1,len-1);
return Math.max(res1,res2);
}
public int houseRobber(int[] nums,int start,int end){
if(start == end)
return nums[start];
if(start +1 == end){
return Math.max(nums[start],nums[end]);
}
if(start +2 == end){
return Math.max(nums[start]+nums[end],nums[start+1]);
}
int len = nums.length;
int[] dp = new int[len];// 打劫 第i个房间,所能够获得最大值
dp[start] = nums[start];
dp[start+1] = nums[start+1];
dp[start+2] = nums[start+2] + dp[start];
for(int s = start + 3;s<= end;s++){
dp[s] = nums[s] + Math.max(dp[s-3],dp[s-2]);
}
return Math.max(dp[end],dp[end-1]);
}
}
打劫房间I中下面定义的dp[i],表示打劫到第i个房间所能够获得0-i内的局部最大值
定义dp[i],表示当前所能获得的最大收获,这里的值最终就是最大值,数组dp的长度是len+1,第一个元素是0,dp[i]也可以理解为,不包含A[i]元素时所取得的最大值
dp[i] = Math.max(dp[i-1],dp[i-2]+A[i-1])
不是很理解
public class Solution {
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public int houseRobber2(int[] nums) {
// write your code here
if(nums==null || nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
if(nums.length == 2)
return Math.max(nums[0],nums[1]);
if(nums.length == 3)
return Math.max(Math.max(nums[0],nums[1]),nums[2]);
int len = nums.length;
int res1 = houseRobber(nums,0,len-2);
int res2 = houseRobber(nums,1,len-1);
return Math.max(res1,res2);
}
public int houseRobber(int[] nums,int start,int end){ int len = nums.length;
int[] dp = new int[len+1];// 打劫 0-i之间的房间,所能够获得最大值
dp[start] = 0;
dp[start+1] = nums[start]; for(int s = start+2;s<=end+1;s++){
dp[s] = Math.max(dp[s-1],dp[s-2]+nums[s-1]);
}
return dp[end+1];
}
}
lintcode:打劫房屋II的更多相关文章
- 打劫房屋 · House Robber
[抄题]: 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警 ...
- lintcode:打劫房屋 III
题目 打劫房屋 III 在上次打劫完一条街道之后和一圈房屋之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子组成的区域比较奇怪,聪明的窃贼考察地形之后,发现这次的地形是一颗二叉树.与前两次偷窃 ...
- lintcode:打劫房屋
题目 打劫房屋 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动 ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LintCode] 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 ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- lintcode:背包问题II
背包问题II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分.你所挑选的 ...
随机推荐
- Python修饰器的函数式编程
Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...
- mysql TRUNCATE
保留小数点 select truncate(field1,2) from table1 field3 字段类型为decimal(20,3)
- [转]What’s Behind Ericsson’s OpenWebRTC Project?
[转]What’s Behind Ericsson’s OpenWebRTC Project? http://www.tuicool.com/articles/z6rAVrJ Ericsson’s O ...
- Android编程: Activity生命周期和LogCat使用
学习内容:Activity生命周期和LogCat使用 ====Activity生命周期==== 图示(转载): 创建 onCreate重启 onRestart开始 onStart恢复 ...
- 黑金开发板上开发的PWM
/*自己做的PWM程序*2014-01-09*/module pwm(clk,rst,led);input clk,rst;output [7:0] led;parameter T=31'd20000 ...
- Linux C 文件与目录3 文件读写
文件读写 文件读写是指从文件中读出信息或将信息写入到文件中.Linux文件读取可使用read函数来实现的,文件写入可使用write函数来实现.在进行文件写入的操作时,只是在文件的缓冲区中操作,可能没有 ...
- C++设计模式——代理模式
前言 青春总是那样,逝去了才开始回味:大学生活也是在不经意间就溜走了,现在上班的时候,偶尔还会怀念大学时,大家在一起玩游戏的时光.大学喜欢玩游戏,但是可悲的校园网,速度能把人逼疯了:还好,后来搞了一个 ...
- mongodb修改器
mongodb修改器 转载自:http://blog.csdn.net/mcpang/article/details/7752736 mongodb修改器(\(inc/\)set/\(unset/\) ...
- VIM技巧:翻页
整页翻页 ctrl-f ctrl-bf=forword b=backward 翻半页ctrl-d ctlr-ud=down u=up 滚一行ctrl-e ctrl-y zz 让光标所在的行居屏幕中央z ...
- 反向Ajax,第2部分:WebSocket
转自:http://kb.cnblogs.com/page/112616/ 前言 时至今日,用户期待的是可通过web访问快速.动态的应用.这一文章系列展示了如何使用反向Ajax(Reverse Aja ...