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均为整数.你不能将物品进行切分.你所挑选的 ...
随机推荐
- Extjs 下拉框下拉选项为Object object
使用Extjs的下拉框出现下拉选项为Object object的问题. 原因在于对store属性提供的是data信息,而不是store对象
- 4 我们的第一个c#程序
1. 控制台应用程序. 在我们这个培训中主要使用控制台应用程序来讲解知识点和做练习. 什么是控制台程序? 控制台程序运行在dos窗口.没有可视化的界面.可以通过Dos窗口进入输入和输出显示 ...
- AngularJs学习笔记-慕课网AngularJS实战
第1章 快速上手 放弃了IE8以及以下,不支持. 4大核心特性: 1.MVC Model: 数据模型 View:视图 Controller:业务逻辑和控制逻辑 好处:职责清晰,模块化. 2.模块化 3 ...
- 用Sqlplus手动创建Oracle11g数据库
用Sqlplus手动创建Oracle数据库 刚开始学习Oracle数据库,菜鸟一个,使用sqlplus创建数据库遇到了很多问题,通过不断地百度,终于创建成功了.所以顺便把整个过程中犯的一些最低级的错误 ...
- P3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队
太水了,背包DP. (转载请注明出处:http://www.cnblogs.com/Kalenda/) ; var n,f,i,j,ans,t,tt:longint; q:array[..] of l ...
- SqlServer正在执行的sql语句
SELECT [Spid] = session_Id ,ecid ,[Database] = DB_NAME(sp.dbid) ,[User] = nt_username ,[Status] = er ...
- Spring MVC常用的注解类
一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描:  二.五大重要的注解类 1.RequestMapp ...
- 21、HierarchyViewer使用记录
1.是啥 HierachyViewer是一种能够方便开发人员了解activity中的布局信息的工具. 2.异常 HierachyViewer在未root过的设备或者低版本的设备是无法使用的 3.怎么办 ...
- Leetcode#115 Distinct Subsequences
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- flex Chrome flash调试时 出现Shockwave flash has crashed的解决办法
在Chrome中输入:chrome://plugins/ PPAPI的Flash Player停用. 使用NPAPI的Flash player. 这里好像没有显示是Debug版本. 但是我在调 ...