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. 关于如何使用javascript监听滚动条滚动事件

    在网页中,通常有一个通往网页顶部的锚点,现在我们就来实现它 Html代码: <a id="scrollup" href="#top" style=&quo ...

  2. laravel 5.5 跨域问题解决方案

    一.laravel-Cors 安装 在终端执行安装命令如下: composer require barryvdh/laravel-cors 添加服务提供商 在Laravel配置文件app.php的pr ...

  3. Building LinkedIn’s Real-time Activity Data Pipeline

    转自:http://blog.163.com/guaiguai_family/blog/static/20078414520138911393767/ http://sites.computer.or ...

  4. SharePoint CAML Query小结

    CAML的结构. <View Type="HTML" Name="Summary"> <ViewBody ExpandXML="TR ...

  5. cyml

    bin/kafka-topics.sh --create --replication-factor 1 --partitions 1 --topic mc_logger2 --zookeeper lo ...

  6. yii源码二 -- interfaces

    path:framework/base/interfaces.php overview:This file contains core interfaces for Yii framework. in ...

  7. SuperMap开发入门1——资源下载

    前言(废话) 由于项目需要,我们将被改用超图(SuperMap)平台进行GIS开发.记忆中,我还是在学生时代使用过超图软件5.0版本,安装包只有50M,这也是超图与学校有合作关系的缘故. 在以后的学习 ...

  8. MicrosoftAjax.js

    MicrosoftAjax.js下载 Function.__typeName = "Function"; Function.__class = true; Function.cre ...

  9. 牛客网-《剑指offer》-从尾到头打印链表

    题目:http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035 C++ /** * struct ListNode { * i ...

  10. SQL Server还原数据库

    http://www.cnblogs.com/ggll611928/p/6377545.html 恢复数据库: 1.分离数据库以断开当前的访问连接. 2.附加数据库mdf文件. 3.执行RESTORE ...