题目

打劫房屋II

在上次打劫完一条街道之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子围成了一个圈,这就意味着第一间房子和最后一间房子是挨着的。每个房子都存放着特定金额的钱。你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警

给定一个非负整数列表,表示每个房子中存放的钱, 算一算,如果今晚去打劫,你最多可以得到多少钱 在不触动报警装置的情况下。

样例

给出nums = [3,6,4], 返回 6, 你不能打劫34所在的房间,因为它们围成一个圈,是相邻的.

解题

打劫房屋I

根据题目,第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的更多相关文章

  1. 打劫房屋 · House Robber

    [抄题]: 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警 ...

  2. lintcode:打劫房屋 III

    题目 打劫房屋 III 在上次打劫完一条街道之后和一圈房屋之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子组成的区域比较奇怪,聪明的窃贼考察地形之后,发现这次的地形是一颗二叉树.与前两次偷窃 ...

  3. lintcode:打劫房屋

    题目 打劫房屋 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动 ...

  4. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  5. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

  6. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  7. [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 ...

  8. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  9. lintcode:背包问题II

    背包问题II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分.你所挑选的 ...

随机推荐

  1. [IOS] Storyboard全解析-第一部分

    (Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果,我贴上本教程所完成的Storyboard的截图: 现在,你就可以清楚的看到这 ...

  2. iOS8 超简单的设置圆角按钮 ImageView等UIView

    button.layer.cornerRadius = // 这个值根据你想要的效果可以更改 button.clipsToBounds = true 这种方法不止可以设置按钮,UIView应该都可以设 ...

  3. 第2章 变量和基本类型 附3---底层const和顶层const

    和英文版的对: As we’ve seen, a pointer is an object that can point to a different object. As a result,we c ...

  4. OA项目实战(二) 开发准备

    上次的博文OA系统实践(一) 概述中,我们已经了解了OA的相关概念.从本次博文开始,我们做一个简单的OA实例. 在OA开发之前,有几个工作们需要提前做.一个是对需求进行分析,另一个是对开发环境进行选择 ...

  5. Win8.1+vs2012+osg环境搭建

    Win8.1+vs2012+osg环境搭建 一.    相关准备 a) Osg源码 当前最新版:OpenSceneGraph-3.2.0.zip 下载链接: http://www.opensceneg ...

  6. html表格属性

    一.在表格中插入文字及图片 1.把图片及文字分开到不同的[tr]标签表格内. <html> <body> <table border="1" widt ...

  7. Differences Between Xcode Project Templates for iOS Apps

    Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xc ...

  8. Android -- 经验分享

    目录                                                                                             代码中安装 ...

  9. xml基础学习笔记05

    Xpath快速解析 如题一样,本篇主要说说Xpath快速查找XML文档   * Xpatn.Xquery,是专门用来查询xml的语言   * 查询xml非常快   Xpatn.Xquery,是专门用来 ...

  10. 运用.NIT将数据存入数据库、读取数据库(运用封装)陈老师作业

    我基础不好,根据所学的知识,书本的例题修改的,也不知道我理解的是否符合老师要求 运用C#将数据存入数据库.并且可以读取数据库里的数据,此项目我运用了封装.我运用了一个窗体将数据存读数据. 我首先创建了 ...