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 指数. ...
随机推荐
- spark[源码]-sparkContext详解[一]
spark简述 sparkContext在Spark应用程序的执行过程中起着主导作用,它负责与程序和spark集群进行交互,包括申请集群资源.创建RDD.accumulators及广播变量等.spar ...
- 【二分+SPFA】修建道路(road)
(四五年以前的老草稿,作为强迫症还是发布出来吧) 修建道路(road.pas/c/cpp) [问题描述] NOIP2012的参赛者LG异想天开打算修建一条磁悬浮列车的通道连接现代OI王国的首都(编号为 ...
- 利用arcgis制作出 源解析要用的ASCII文件
准备:1.确定好模拟区域范围,精度,行列数 2.确定好源解析的城市规划 思路: 1.全国省级图+本地市县图-->合成一张区域图(联合) 合成之后,添加一个字段,一个数字类型字段 ...
- Http:UTF-8与GB2312之间的关系
UTF-8里包括GB2312.UTF-8是国际通用的标准(包括世界所有的语言),而GB2312(只是简体中文)只适合做中文的网站.假设你想做个中文网页,但是还可以翻成英文的话,就得用UTF-8.如果用 ...
- Docker-docker镜像
前言 在 Docker 1.13+ 版本中推荐使用 docker image 来管理镜像. 查看安装的Docker版本信息: [dockuser@localhost Desktop]$ docker ...
- https nginx配置
cd /saas/conf/nginx/ mkdir key cd key 创建key: openssl req -nodes -newkey rsa:2048 -keyout server.key ...
- 20145327 《Java程序设计》第九周学习总结
20145327 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无需接触底层 ...
- Java GC垃圾回收
Java的内存分配和回收也主要在Java的堆上进行的,Java的堆中存储了大量的对象实例,所以Java的堆也叫GC堆. Java在垃圾收集的过程中,主要用到了分代收集算法,具体有复制.标记清除.标记压 ...
- Juniper SRX防火墙简明配置手册(转)
在执行mit命令前可通过配置模式下show命令查看当前候选配置(Candidate Config),在执行mit后配置模式下可通过run show config命令查看当前有效配置(Active co ...
- 记jsp判断
empty:表示空字符串,null,空数组,空集合. ! empty:表示非空字符串,非null,非空数组,非空集合.