Question

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.

Solution

Key to this problem is to break the circle. So we can consider two situations here:

1. Not include last element

2. Not include first element

Therefore, we can use the similar dynamic programming approach to scan the array twice and get the larger value.

 public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length < 1)
return 0;
int length = nums.length, tmp1, tmp2;
if (length == 1)
return nums[0];
int[] dp = new int[length];
dp[0] = 0;
dp[1] = nums[0];
// First condition: include first element, not include last element;
for (int i = 2; i < length; i++)
dp[i] = Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]);
tmp1 = dp[length - 1];
// Second condition: include last element, not include first element;
dp = new int[length];
dp[0] = 0;
dp[1] = nums[1];
for (int i = 2; i < length; i++)
dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]);
tmp2 = dp[length - 1];
return tmp1 > tmp2 ? tmp1 : tmp2;
}
}

House Robber II 解答的更多相关文章

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

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

  2. 198. House Robber,213. House Robber II

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

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

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

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

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

  5. 【LeetCode】213. House Robber II

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

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

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

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

  8. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

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

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

随机推荐

  1. 北京Uber优步司机奖励政策(12月3日)

    用户组:人民优步(适用于12月3日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://w ...

  2. 【转】Linux下socket keep alive讲解

    [需求]不影响服务器处理的前提下,检测客户端程序是否被强制终了.[现状]服务器端和客户端的Socket都设定了keepalive属性.服务器端设定了探测次数等参数,客户端.服务器只是打开了keepal ...

  3. 【多线程】--生产者消费者模式--Lock版本

    在JDK1.5发布后,提供了Synchronized的更优解决方案:Lock 和 Condition 我们使用这些新知识,来改进例子:[多线程]--生产者消费者模式--Synchronized版本 改 ...

  4. [Regular Expressions] Find the Start and End of Whole Words

    Regular Expression Word Boundaries allow to perform "whole word only" searches within our ...

  5. arcengine 调用arctoolbox功能的举例 spatialJoin

    废话不多说,code是王道. 其中str1.str2两个参数是target路径.join路径 private void spatialJoin(Geoprocessor gp, string str1 ...

  6. 配置IIS

    1.aspnet_regiis -i 重新安装IIs  vs2013的命令符 2. 分配权限 1.对文件夹,权限,安全,分配权限 2.设置webconfig 对应的httpModel 及安全性 3.设 ...

  7. Linux中oracle安装时候报ora-00119解决办法

    ORA-00119: invalid specification for system parameter LOCAL_LISTENER ORA-00130: invalid listener add ...

  8. html简单样式

    1.外部样式表 link rel="stylesheet" type="text/css" href="bbb.css"> 2.内部样 ...

  9. HTML5简单入门系列(九)

    前言 上篇本来应该就是最后一篇了,但是楼主总觉得没有写上一个简单应用,不算是完整的学习系列.所以增加一篇关于动画的应用,对一个开源动画的介绍(很基础,非楼主原创). 本篇介绍一个基于Canvas的发光 ...

  10. ThreadLocal的使用 .

    早在Java 1.2推出之时,Java平台中就引入了一个新的支持:java.lang.ThreadLocal,给我们在编写多线程程序时提供了一种新的选择.使用这个工具类可以很简洁地编写出优美的多线程程 ...