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. android动手写控件系列——老猪叫你写相机

    前记:Android这个开源而自由的系统,为我们带来开发便利,同时也埋下太多的深坑.例如调用系统自带的相机就会出现照片丢失,或者其他各种各样的问题.因此,看来自定义一个相机十分的必要. 要自定义相机我 ...

  2. Centos安装FTP服务器和配置

    安装 yum install vsftpd 启动/重启/关闭 /sbin/service vsftpd start /sbin/service vsftpd restart /sbin/service ...

  3. SCIKIT-LEARN与GBDT使用案例

    http://blog.csdn.net/superzrx/article/details/47073847 安装 SCIKIT-LEARN是一个基于Python/numpy/scipy的机器学习库  ...

  4. Java-JUC(七):同步锁的几种方式

    为什么要使用同步锁? 因为当使用多线程同时访问一个变量或对象时,如果这些线程中即有读又有写操作时,会造成导致变量或对象的状态出现混乱.例如:一个银行账户被A/B两个线程同时操作,A线程.B线程同时开始 ...

  5. Eclipse技术: 项目文件中过滤.o文件

    1. 右建项目 -> Properties. 2. 增加过滤规则

  6. pip通过requirements.txt安装依赖

    pip install -t . -r requirements.txt requirements.txt Keras==2.0.8 gensim torchvision opencv-python ...

  7. 从n个数中随机选取m个

    咋一看,这是个很简单的问题,但是如果n是个不确定的数呢?比如服务器每天会收到数以亿计的请求,但是目前服务器端不希望保存所有的请求,只想随机保存这些请求中的m个.试设计一种算法,能够使服务器实时保存m个 ...

  8. ANT task之Junit、JunitReport

    一.ANT任务之Junit: 学习ANT其实主要是学习ANT的task,ANT众多task中有一个Testing Tasks,它下面有两个任务:Junit和JunitReport,主要用来进行单元测试 ...

  9. Go语言中异常处理painc()和recover()的用法

    Go语言中异常处理painc()和recover()的用法 1.Painc用法是:用于抛出错误.Recover()用法是:将Recover()写在defer中,并且在可能发生panic的地方之前,先调 ...

  10. ssi框架搭建

    Struts2主要来源于webwork框架,与Struts1相比,在数据传递方面,Struts2提供了更加强大OGNL标签功能,使其能够通过在action中定义变量来直接与jsp页面中的数据进行相互传 ...