【LeetCode】213. House Robber II
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的更多相关文章
- 【LeetCode】213. House Robber II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
随机推荐
- 使用 GNU Libtool 创建库
这篇文档向大家介绍 GNU Libtool 的用途及基本使用方法,同时描述如何结合 GNU Autoconf 和 Automake 来使用 Libtool. 3 评论: 吴 小虎, 程序员, 天用唯勤 ...
- Flask刷新问题
修改页面中内容,特别是图片后,总是刷新不了.调试时,我常常通过修改端口来解决,从80-99不断改. 服务器部署,也遇到同样问题,重启web服务器,重启计算机都不行,网页已经改过来了,但是图片还是老图片 ...
- [Canvas]碰撞球
观赏动态效果请点此下载并用Chrome/Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8" ...
- java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)
拂晓风起 专注前端技术cocos2d.js.flash.html5,联系:kenkofox@qq.com.如果读者要找腾讯工作机会,请不吝推荐简历. 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔 ...
- java enum 用法
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or th ...
- Python 函数参数引用(传值/传址)/copy/deepcopy
精简版: 传值:被调函数局部变量改变不会影响主调函数局部变量 传址:被调函数局部变量改变会影响主调函数局部变量 Python参数传递方式:传递对象引用(传值和传址的混合方式),如果是数字,字符串,元组 ...
- vue build后运行错误
events.js:141 throw er; // Unhandled 'error' event 这个是端口占用的问题 $ http-server dist events.js:141 throw ...
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
近期更新了一下HttpClientUtil工具类代码,主要是加入了一个參数HttpContext,这个是用来干嘛的呢?事实上是用来保存和传递Cookie所须要的. 由于我们有非常多时候都须要登录.然后 ...
- java动态代理_aop
(一)代理概述 1.问题:要为已存在的多个具有相同接口的目标类的各个方法增加一些系统功能,例如,异常处理.日志.计算方法的运行时间.事务管理等等,如何去做? 解答:编写一个与目标类具有相同接口的代理类 ...
- BIEE Demo(RPD创建 + 分析 +仪表盘 )
说明:此Demo步骤简略,详细Demo可以参照下面的 天善视频:BIEE 11G Rpd模型设计 天善视频:BIEE 11G 报表开发 Oracle BIEE (Business Intelligen ...