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. iOS设备、Icon、LaunchImage、图片分辨率

    iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...

  2. 3D math primer for graphics and game development

    三角网格(Triangle Mesh) 最简单的情形,多边形网格不过是一个多边形列表:三角网格就是全部由三角形组成的多边形网格.多边形和三角网格在图形学和建模中广泛使用,用来模拟复杂物体的表面,如建筑 ...

  3. ACM HDU 1021 Fibonacci Again

    #include<iostream> using namespace std; int main() { int n; while(cin>>n) { if((n+1)%4== ...

  4. struts-json

    Struts2序列化的属性,该属性在action中必须有对应的getter方法 如果action的属性很多,我们想要从Action返回到调用页面的数据.这个时候配置includeProperties或 ...

  5. WPF提示框效果

    WPF提示框效果 1,新建WPF应用程序 2,添加用户控件Message 3,在Message中编写如下代码 <Border x:Name="border" BorderTh ...

  6. java 使用substring 截取特殊字符串的后一位或者数字

    关于截取特殊的字符串的后一位或者数字 需求:截取特殊字符为  .   后一位 String[] str = uri.split("/"); String str1 = str[st ...

  7. 生产环境CentOS服务器系统安全配置

    转http://www.centoscn.com/CentosSecurity/CentosSafe/2014/1126/4192.html 账户安全及权限 禁用root以外的超级用户 删除不必要的账 ...

  8. PHP获取当前页面完整url地址,包括参数的函数

    //php获取当前访问的完整url地址 function get_current_url(){     $current_url='http://';     if(isset($_SERVER['H ...

  9. How To mount/Browse Windows Shares【在linux{centos}上挂载、浏览window共享】

    How to mount remote Windows shares Contents Required packages Basic method Better Method Even-better ...

  10. 在IT学习中的“认识论”

    先有“感性认识”,再有“理性认识”.这句经典认识论,在IT领域应用广泛. 一个项目,首先是分析需求,也就是功能层面,这相当于先有“感性认识”:然后才是具体的技术实现,也就是“理性认识”. IT学习分析 ...