LeetCode OJ:House Robber II(房屋窃贼II)
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 arearranged 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.
这一题和前面的那个窃贼问题比较类似,不过这里首位同样判定是相邻的,所以这里的做法应该是先求出第一个到倒数第二个的最大值,在求出第二个到最后一个的最大值,两者较大的就是最大的值了,同样用dp来解决,代码如下所示:
class Solution {
public:
int rob(vector<int>& nums) {
if(!nums.size()) return ;
if(nums.size() == ) return nums[];
vector<int> ret1, ret2;
ret1.resize(nums.size());
ret2.resize(nums.size());
ret1[] = nums[];
ret2[] = nums[];
for(int i = ; i < nums.size() - ; ++i){
ret1[i] = max((i == ? : ret1[i - ]) + nums[i], ret1[i - ]);
}
for(int i = ; i < nums.size(); ++i){
ret2[i] = max((i == ? : ret2[i - ]) + nums[i], ret2[i - ]);
}
return max(ret1[nums.size() - ], ret2[nums.size() - ]);
}
};
LeetCode OJ:House Robber II(房屋窃贼II)的更多相关文章
- LeetCode OJ 95. Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- <LeetCode OJ> 78 / 90 Subsets (I / II)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- Leetcode之二分法专题-275. H指数 II(H-Index II)
Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...
随机推荐
- JSON 转 对象
Json对象与Json字符串的转化.JSON字符串与Java对象的转换 一.Json对象与Json字符串的转化 1.jQuery插件支持的转换方式: $.parseJSON( jsonstr ); ...
- windows7上安装php7和apche2.4
windows7在配置php7+apache2.4 1.下载并安装vc14http://www.microsoft.com/zh-cn/download/details.aspx?id=48145下载 ...
- 机器学习与R语言:NB
#---------------------------------------- # 功能描述:演示NB建模过程 # 数据集:SMS文本信息 # tm包:维也纳财经大学提供 #----------- ...
- Django---ModelForm详解
示例: from django.db import models from django.forms import ModelForm TITLE_CHOICES = ( ('MR', 'Mr.'), ...
- 20145302张薇《Java程序设计》第三周学习总结
20145302张薇<Java程序设计>第三周学习总结 教材学习内容总结 第四章 定义类 一个原始码中有多少类就会有多少.class文档. 标准类 使用java.util.scanner让 ...
- (java) 第二周学习总结
在java源代码中,每个变量都必须声明一种类型(type).有两种类型:primitive type和reference type.引用类型引用对象(reference to object),而基本类 ...
- JQuery中serialize()
一.serialize()定义和用法: serialize()方法通过序列化表单值,创建标准的URL编码文本字符串,它的操作对象是代表表单元素集合的jQuery 对象.你可以选择一个或多个表单元素(比 ...
- python正则表达式 Python Re模块
最近在学python 练习的时候随手写的,方便以后自己参考~如果能对其他同学有所帮助就再好不过了 希望大家指正哦~ 我会随时整理的,先这样~ 正则表达式 1.元字符([ ]),它用来指定一个char ...
- ThinkPHP开发笔记-视图
1.如果要在模板中输出变量,必须在在控制器中把变量传递给模板,系统提供了assign方法对模板变量赋值,无论何种变量类型都统一使用assign赋值,而且assign方法必须在display和show方 ...
- 对dataframe中某一列进行计数
本来是一项很简单的任务...但很容易忘记搞混..所以还是记录一下 方法一: df['col'].value_counts() 方法二: groups = df.groupby('col') group ...