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. git分支使用

    1.查看远程服务器分支 git branch -a 2.查看本地分支 git branch 3.切换分支 git checkout master 4.删除远程分支 git push origin :b ...

  2. iOS沙盒机制的基本操作总结

    每个ios程序都有自己的沙盒(sandBox),ios8之后提供沙盒部分开放 我们可以访问沙盒下的文件夹 文件夹包括: 1,documents:保存应用运行时生成的需要持久化的数据 2.tem:保存临 ...

  3. 用js实现图片自动加载的瀑布流效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. Java学习笔记(八)——封装

    一.封装 1.定义 将类的信息隐藏在类的内部,不允许外部程序直接进行访问,而是通过该类提供的方法来实现对隐藏信息的操作和方法. 2.优点 (1)只能通过规定的方法访问数据 (2)隐藏类的细节,方便修改 ...

  5. SQL Server 创建表 添加主键 添加列常用SQL语句

    --删除主键 alter table 表名 drop constraint 主键名 --添加主键 alter table 表名 add constraint 主键名 primary key(字段名1, ...

  6. Intent界面跳转与传递数据

    Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. intent可以激活Activity,服务,广播三类组件. 本博文讲的是显示意图激活Activity组件 ...

  7. Android学习系列(39)--Android主题和样式之系统篇(上)

    [基于最新的Android4.4的源码分析] 每家公司或者每个移动团队无不想开发出一套自己的UI框架,融入自己的设计和特性,这必然会去修改android的ui.所以,学习和理解android的UI设计 ...

  8. PHP学习之常量

    1.常量是一个简单值的标识符,该值在脚本中不能改变: 2.一个常量由英文字母,下划线,和数字组成,但数字不能作为首字母出现:(常量名中不需要加$修饰符) 3.常量在整个脚本中都可以使用: 4.设置PH ...

  9. js-变量、作用域和内存问题,引用类型

    变量.作用域和内存问题 1.变量可能包含两种不同数据类型的值:基本类型值以及引用类型值:引用类型值保存的是内存中的对象 2.对象是按值传递的, function setName(obj){ obj.n ...

  10. 泛型容器单元(Generics.Collections)[2]: TQueue<T> 队列列表

    TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TQueue 主要有三个方法.一个属性:Enqueue(入列).Dequeue(出列).Pee ...