House Robber II

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.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

House Robber的差异在于,nums[0]和nums[n-1]不能同时包含,

因此等同于nums[0...n-2]和nums[1...n-1]两者间取较大值。

class Solution {
public:
int rob(vector<int>& nums) {
if(nums.empty())
return ;
if(nums.size() == )
return nums[];
vector<int> nums1(nums);
vector<int> nums2(nums);
nums1.erase(nums1.begin());
nums2.pop_back();
return max(originRob(nums1), originRob(nums2));
}
int originRob(vector<int>& nums)
{
if(nums.empty())
return ;
int prev2 = ;
int prev1 = nums[];
for(int i = ; i < nums.size(); i ++)
{
int cur = max(prev2+nums[i], prev1);
prev2 = prev1;
prev1 = cur;
}
return prev1;
}
};

【LeetCode】213. House Robber II的更多相关文章

  1. 【LeetCode】213. House Robber II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...

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

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

  3. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  4. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  6. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  7. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  8. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

随机推荐

  1. Python Numpy模块函数np.c_和np.r_

    np.r_:是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat(). np.c_:是按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等,类似于pandas中的 ...

  2. 深度学习(Deep Learning)算法简介

    http://www.cnblogs.com/ysjxw/archive/2011/10/08/2201782.html Comments from Xinwei: 最近的一个课题发展到与深度学习有联 ...

  3. 怎样将 MySQL 迁移到 MariaDB 上

    自从甲骨文收购 MySQL 后,由于甲骨文对 MySQL 的开发和维护更多倾向于闭门的立场,很多 MySQL 的开发者和用户放弃了它.在社区驱动下,促使更多人移到 MySQL 的另一个叫 MariaD ...

  4. VS2017专业版使用最新版Qt5.9.2教程

    VS2017专业版使用最新版Qt5.9.2教程(最新教材) 最近三天一直在安装Qt5.9.2,为了能够在自己专业版的VS2017上面使用?可以算是花费了不少的功夫.但是一路上并不是很顺利,就在刚才,终 ...

  5. EFI/UEFI BIOS启动

    全局唯一标识分区表(GUID Partition Table,缩写:GPT)是一个实体硬盘的分区结构.它是可扩展固件接口标准的一部分,用来替代BIOS中的主开机纪录分区表.但因为MBR分区表不支持容量 ...

  6. WCF服务引用之后自动生成的泛型代理类名称太长的解决方案

    问题:WCF服务引用之后会将原来的泛型类自动生成一个代理类,但是有时候名称太长怎么办? 解决方案: 1.方案一: 调用客户端同样也引用这个泛型类的类库. 2.方案二: 找到这个泛型类,然后在上面的[D ...

  7. 1049: 贝贝的车牌问题(car)

    http://oj.lcsyzx.cn/JudgeOnline/problem.php?id=1049 var a : ]; m,n,t : integer; begin readln(m); n:= ...

  8. 使用gradle的application插件进行Spring-boot项目打包

    1:在build.gradle中增加以下配置 fat jar并不总是一个合适的选择,比如需要依赖跟jar分离,使用gradle的application插件就可以做到. 在GradleTest项目中,b ...

  9. Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL

    1:Winform应用通过mshtml操作IE浏览器DOM时,第一次运行正常,点击第二次时错误信息如下 A first chance exception of type 'System.Runtime ...

  10. 获取自增ID

    方法一(一般不使用,多线程可能不准确) SELECT MAX(id) FROM table 方法二(一般使用这个) SELECT LAST_INSERT_ID()