【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 ...
随机推荐
- 解决EditPlus在设置了UTF-8之后,编写的HTML页面仍出现汉字乱码问题
解决EditPlus在设置了UTF-8之后.编写的HTML页面仍出现汉字乱码问题 相信有些同学在使用EditPlus编写HTML页面时发现,尽管已经设置好了UTF-8的编码格式.但却发现HTML页 ...
- lstm(一) 演化之路
递归神经网络引入了时序的反馈机制,在语音.音乐等时序信号的分析上有重要的意义. Hochreiter(应该是Schmidhuber的弟子)在1991年分析了bptt带来的梯度爆炸和消失问题,给学习算法 ...
- mongoose系列——几行代码实现CRUD
1. nodejs 确实好用,mongoose封装了mongodb,代码很简洁. const mongoose = require('mongoose'); mongoose.connect(&quo ...
- RedisTemplate 分页
利用spring redis的RedisTemplate进行分页: 场景: 现有项目若干,根据项目的创建时间(createTime)进行降序读取: 存储结构: key:proList(list) 存放 ...
- PDO 增删改查封装的类
Selecting Data 你在mysql_*中是这样做的 <?php $result = mysql_query('SELECT * from table') or die(mysql_er ...
- Sublime Text 3 配置 PHPCS 插件
Download php code sniffer addon via Package Control in ST3. Download The php-cs-fixer File From This ...
- android中使用WebView请求本地网页
使用WebView的方式请参考我的上一篇文章:android中使用WebView请求网页 这里说一下请求本地网页的方法: 本地网页应该把网页保存在src/main/assets目录下: webView ...
- android中使用setOnKeyListener响应输入事件
在界面中添加一个EditText输入框控件,需要在输入时响应输入事件,可以使用setOnKeyListener() 事件: final EditText editText = findViewById ...
- Android 如何关闭Navigation Bar M
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 使用devenv/MSBuild在命令行编译sln或csproj
一 使用devenv来build单个project devenv是VisualStudio的可执行程序,一般安装在“C:\Program Files (x86)\Microsoft Visual ...