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.

题目大意:所有的住户围成一圈,只能隔着抢劫,问可以抢到的总额最大是多少。

解题思路:

因为第一家和最后一家是连着的,所以抢第一家就不能抢最后一家,抢最后一家就不能抢第一家,那么可以把数组分成两段,0~n-1和1~n两段,分别计算最大的,取较大的。

    public int rob(int[] nums) {
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]);
}
int res = -1;
int[] max = new int[nums.length+1];
max[0]=nums[0];
max[1]=Math.max(nums[0],nums[1]);
for(int i=2;i<nums.length-1;i++){
max[i]=Math.max(max[i-1],max[i-2]+nums[i]);
}
res=Math.max(res,max[nums.length-2]);
Arrays.fill(max,0);
max[1]=nums[1];
max[2]=Math.max(nums[1],nums[2]);
for(int i=3;i<nums.length;i++){
max[i]=Math.max(max[i-1],max[i-2]+nums[i]);
}
res=Math.max(res,max[nums.length-1]);
return res;
}

House Robber II——Leetcode的更多相关文章

  1. Housse Robber II | leetcode

    可以复用house robber的代码,两趟dp作为两种情况考虑,选最大值 #include <stdio.h> #define MAX 1000 #define max(a,b) ( ( ...

  2. [LeetCode]House Robber II (二次dp)

    213. House Robber II     Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...

  3. LeetCode之“动态规划”:House Robber && House Robber II

    House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...

  4. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  5. 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:二叉树下的不能相邻,求能 ...

  6. 【刷题-LeetCode】213. House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

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

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

  8. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  9. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

随机推荐

  1. Cisco AnyConnect “Failed to initialize connection subsystem”的解决方案

    Per Cisco: Microsoft has released a fix-it patch providing a workaround for this issue. See KB# 3023 ...

  2. Object-C Init

    上一篇为Object-C类实现 我们可以创建一个init方法用来给我们的实例变量设置初始化值: - (id)init { if(self = [super init]) { [self setCapt ...

  3. 类库探源——System.Environment

    Environment 类: 提供有关当前环境和平台的信息以及操作它们的方法.此类不能被继承. 命名空间: System 程序集:   mscorlib.dll 继承关系: 常用属性(字段)和方法: ...

  4. js日期格式,日期对象

    以对象为基准去使用方法, 围绕Date对象 var a = new Date() 返回当前的时间对象,可以使用内置的日期对象的方法 a.getFullYear(), a.getMonth(), a.g ...

  5. php 连接mysql数据库并显示数据 实例 转载

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. IE9透明filter和opacity同时生效的解决办法 IE9 hack only

    转载请注明:来自于http://www.cnblogs.com/bluers/ 问题: 假设结构如下: <div class="wrapper"> <p clas ...

  7. 复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html

    {php $specials = explode(',',$r[special]);} <div class="special"> {loop $specials $s ...

  8. 使用JsPlumb绘制拓扑图的通用方法

    转自:http://www.it165.net/pro/html/201311/7616.html 使用JsPlumb绘制拓扑图的通用方法 一. 实现目标 绘制拓扑图, 实际上是个数据结构和算法的问题 ...

  9. C#一个字符串的加密与解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  10. QML之TextEdit

    TextEdit显示一个可编辑的,有格式的文本框.它也可以显示明文和富文本.例如:TextEdit {    width: 240    text: "<b>Hello</ ...