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. [置顶] java ant 配置及构建项目

      Ant是一种基于Java的构建工具.Ant文件是配置构建目标过程的XML文件,也称为Ant脚本.                     (因为对这个不是很了解,所以用词方面可能于个人的理解有偏差 ...

  2. storm源代码分析---Transactional spouts

    Transactionalspouts Trident是以小批量(batch)的形式在处理tuple.而且每一批都会分配一个唯一的transaction id.不同spout的特性不同,一个trans ...

  3. iOS经常使用的加密算法

    在iOS开发中,为了数据的安全经常对内容进行加密,在这儿我们对经常使用的加密算法进行了总结: 1.MD5 <span style="font-size:18px;">+ ...

  4. poj3358数论(欧拉定理)

    http://poj.org/problem?id=3358 (初始状态为分数形式)小数点进制转换原理:n / m ; n /= gcd( n , m ) ; m/= gcd( n , m ) ; n ...

  5. android studio github 项目导入问题

    在github上面看到一个比较好的项目,导入出现了一些问题,记录如下: 项目演示效果如图:下载地址:https://github.com/asijack/PagerSlidingTabStrip 如果 ...

  6. [springmvc+mybatis][关于这两个框架的学习,我想说]

    关于学习笔记 在对java web有了一定的了解后,这两个框架没怎么写学习笔记了…毕竟项目驱动型…… 关于学习资料 强烈推荐传智播客的燕青讲解的 让我对这种培训班教育的资料刮目相看(不过还是千万别去这 ...

  7. [HeadFist-HTMLCSS学习笔记][第四章Web镇之旅]

    重要 访问一个目录,即是访问他的index <a>链接到网站,必须加http:// <a>的title属性,能预先知道链接信息 id属性 使得<a> 能再本地跳转. ...

  8. Html5移动端页面布局通用模板暨移动端问题总结

    最近的移动端项目终于告一段落了,其中遇到了不少问题,在此和大家总结分享. 首先,说一下结构.一般的手机页面大致可以分为五块:head, content, foot,solidbar,dialog. 针 ...

  9. SqlServer存储过程传入Table参数

    今天是周日,刚好有空闲时间整理一下这些天工作业务中遇到的问题. 有时候我们有这样一个需求,就是在后台中传过来一个IList<类>的泛型集合数据,该集合是某个类的实例集合体,然后将该集合中的 ...

  10. oracle定时备份与删除N天前备份文件

    oracle定时备份数据库,以及删除7天前备份的数据. 1.创建存放备份目录: mkdir /home/oracle/data_backup mkdir /home/oracle/log_backup ...