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. [MongoDB]count,gourp,distinct

    摘要 上篇文章介绍了CRUD的操作,会了这些,基本上可以完成很多工作了.但如果遇到统计类的操作,那么就需要学习下本篇的内容了. 相关文章 [MongoDB]入门操作 [MongoDB]增删改查 cou ...

  2. sql server 得到数据库字典

    SELECT      表名=case   when   a.colorder=1   then   d.name   else   ''   end,    表说明=case   when   a. ...

  3. 一个简单的html5页面在线速成工具!(当然本文主要说下他的成果的结构)

    分享一个好玩的web app页面速成工具 当然主要是让大家看下他的原理 看着他的结构大家就该猜到这个了.这个是利用换页之后给当前div加了一个active,然后利用css控制效果 这个毫无疑问是采用最 ...

  4. [译]git reflog

    用法 git reflog 显示整个本地仓储的commit, 包括所有branch的commit, 甚至包括已经撤销的commit, 只要HEAD发生了变化, 就会在reflog里面看得到. git ...

  5. java练手 韩信点兵

    Problem C 韩信点兵 时间限制:3000 ms  |  内存限制:65535 KB   描述 相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排.五人一排.七人一排地变换队 ...

  6. POJ 1265 Area

    有一种定理,叫毕克定理....                             Area Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  7. Marsedit 破解版下载(3.5.6)

    Marsedit 破解版下载(3.5.6) 最近在 Mac 端写文章经常遇到棘手的问题,最近发现了 marsedit 现在分享给大家 marsedit downloads

  8. 【C语言入门教程】4.7 指针的地址分配 - mallocl(), free()

    指针变量可指向任何类型的变量,在处理过程中,指针变量指向的变量通过传递变量的地址来实现,指针变量的取值是内存的地址,这个地址应当是安全的,不可以是随意的.否则,写入内存单元的值将使得已存放的数据或程序 ...

  9. Swift2.1 语法指南——泛型

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  10. linux 驱动 工作队列

    http://blog.sina.com.cn/s/blog_78d30f6b0102uyaf.html http://blog.csdn.net/lyc_stronger/article/detai ...