Note: This is an extension of House Robber.

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.

解题思路:

可以分解为两种情况:包含nums[0]和不包含nums[0],分别求解算出最大即可,JAVA实现如下;

    public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
else if (nums.length <= 2)
return Math.max(nums[0], nums[nums.length - 1]);
else if(nums.length==3)
return Math.max(nums[0], Math.max(nums[1],nums[2]));
int res = 0;
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[2] = nums[0] + nums[2];
dp[3] = nums[0] + nums[3];
for (int i = 4; i < nums.length-1; i++)
dp[i] = nums[i] + Math.max(dp[i - 2], dp[i - 3]);
res=Math.max(dp[nums.length-2],dp[nums.length-3]);
dp[1] = nums[1];
dp[2] = nums[2];
dp[3] = nums[3] + nums[1];
for (int i = 4; i < nums.length; i++)
dp[i] = nums[i] + Math.max(dp[i - 2], dp[i - 3]);
return Math.max(res, Math.max(dp[nums.length-1],dp[nums.length-2]));
}

Java for LeetCode 213 House Robber II的更多相关文章

  1. [LeetCode] 213. House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  2. [LeetCode] 213. House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. LeetCode 213. House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  4. [leetcode] #213 House Robber II Medium (medium)

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

  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. 198. House Robber,213. House Robber II

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

  7. 【LeetCode】213. House Robber II

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

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

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

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. C#对HTML文档的解析

    http://www.2cto.com/kf/201312/268777.html http://jingyan.baidu.com/article/7e44095334bb162fc0e2efad. ...

  2. HTML5+ 学习笔记3 storage.增删改查

    //插入N条数据 function setItemFun( id ) { //循环插入100调数据 var dataNum = new Number(id); for ( var i=0; i< ...

  3. 基础知识系列☞MSSQL→约束

    遇到一个数据库设计很渣的系统··· 本来一个约束就能解决的问题·以前建库的时候也不设计好···

  4. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  5. hessian接口参数,子类与父类不能有同名字段解决方法

    hessian默认是使用 com.caucho.hessian.io.JavaSerializer 序列化,同名字段子类字段值被赋值两次,最终用父类null值赋给了子类同名字段,解决方法就是 指定序列 ...

  6. 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛

    题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...

  7. 将json返回的日期格式转换

    <script>         function ChangeDateFormat(cellval) {             var date = new Date(parseInt ...

  8. Linux中source是什么指令?

    命令用法: source FileName 作用:在当前bash环境下读取并执行FileName中的命令. 注:该命令通常用命令“.”来替代. 如:source /etc/profile 与 .  / ...

  9. linux之vim编辑神器

    设置编辑器:export EDITOR=vim 1.ctrl+x ctrl+e  创建vim临时文件 2.ctrl+[ 退回到命令模式 3.命令模式下ZZ,保存退出 4.大写I行首插入,大写A行尾插入 ...

  10. javascript高级程序设计---Event对象二

    鼠标事件 事件种类 鼠标事件指与鼠标相关的事件,主要有以下一些. (1)click事件 click事件当用户在Element节点.document节点.window对象上,单击鼠标(或者按下回车键)时 ...