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.
Notice

This is an extension of House Robber.

Example

nums = [3,6,4], return 6

LeetCode上的原题,请参见我之前的博客House Robber II

解法一:

class Solution {
public:
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
int houseRobber2(vector<int>& nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
vector<int> a = nums, b = nums;
a.erase(a.begin()); b.pop_back();
return max(rob(a), rob(b));
}
int rob(vector<int> &nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
vector<int> dp{nums[], max(nums[], nums[])};
for (int i = ; i < nums.size(); ++i) {
dp.push_back(max(dp[i - ] + nums[i], dp[i - ]));
}
return dp.back();
}
};

解法二:

class Solution {
public:
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
int houseRobber2(vector<int>& nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
return max(rob(nums, , nums.size() - ), rob(nums, , nums.size()));
}
int rob(vector<int> &nums, int left, int right) {
int a = , b = ;
for (int i = left; i < right; ++i) {
int m = a, n = b;
a = n + nums[i];
b = max(m, n);
}
return max(a, b);
}
};

[LintCode] House Robber II 打家劫舍之二的更多相关文章

  1. [LeetCode] House Robber 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. [LintCode] House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

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

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

  5. 213 House Robber II 打家劫舍 II

    注意事项: 这是 打家劫舍 的延伸.在上次盗窃完一条街道之后,窃贼又转到了一个新的地方,这样他就不会引起太多注意.这一次,这个地方的所有房屋都围成一圈.这意味着第一个房子是最后一个是紧挨着的.同时,这 ...

  6. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

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

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

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

  9. [LeetCode] House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

随机推荐

  1. javascript 简单加解密

    //加密 function MyEncrypt(txt) { var sb = ""; var rand = 0; for (var i=0;i<txt.length;i++ ...

  2. ObCallback回调钩子检测

    ObCallback回调钩子检测 2013-12-20 Nie.Meining Ring0 在 PatchGuard 的摧残下,通过 ObRegisterCallbacks 函数注册回调钩子已经成了 ...

  3. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  4. 关于C/C++中数组元素的初始化

      1.一维数组初始化 对于一维数组,其初始化有一个错误的观点是:一直以为 int a[256]={0};是把a的所有元素初始化为0,int a[256]={1};是把a所有的元素初始化为1. 数组可 ...

  5. 常用chrome插件推荐

    下面打红色的2个强烈推荐使用: FQ的: https://chrome.google.com/webstore/detail/ecross-free/njdjpgffklilbojbobbfecfcg ...

  6. TestNg依赖配置基础用法(单一方法依赖)------TestNg依赖详解(一)

    TestNg依赖测试之简单方法依赖,通过dependsOnMethods属性来配置依赖方法 原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong J ...

  7. Dev 控件问题多少

    150702  编译BPL_MonitorView.bpl包发现提示找不到cxBarEditItemD16包, =>我将此包和往下的 dxSkin**包都尝试性删除,编译提示要加载包,点击确定, ...

  8. HDU5812 Distance(枚举 + 分解因子)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5812 Description In number theory, a prime is a ...

  9. COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)

    题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...

  10. POJ3013 Big Christmas Tree(最短路径树)

    题目大概说给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和. 要求∑(边权*子树点权和),等价于 ...