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 指数. ...
随机推荐
- JS正则表达式从入门到入土(1)—— REGEXP对象
REGEXP对象 JavaScript通过内置对象RegExp支持正则表达式,有两种方法实例化RegExp对象. 1.字面量 2.构造函数 字面量 字面量是直接通过/.../创建RegExp对象实例. ...
- Ant Design 常用命令汇总
Ant Design React 安装 1. 安装脚手架工具# antd-init 是一个用于演示 antd 如何使用的脚手架工具,真实项目建议使用 dva-cli. $ npm install an ...
- 一个url加载的全过程
最近在进行前端面试方面的一些准备,遇到了一个经典前端问题,一个url从输入到页面加载中间到底发生了什么,以前也认真想过这个问题,但是当时回答的都不全面,现在来好好总结一下: 总体来说分为以下六个步骤: ...
- ELK之elasticsearch5.6的安装和head插件的安装
这里选择的elasticsearch为5.6的新版本,根据官方文档有几种暗装方式: https://www.elastic.co/guide/en/elasticsearch/reference/cu ...
- 重新想,重新看——CSS3变形,过渡与动画④
最后,我们来探讨一下CSS3的动画属性. 之前提到过,实际上过渡也算作动画的一种.但过渡作为动画的缺陷在于,只能使元素属性从一个值“过渡”至另一个值,但如果想要使元素的属性值根据需要在时间轴上不断变化 ...
- 4.9版本的linux内核中实时时钟芯片pt7c4338的驱动源码在哪里
答:drivers/rtc/rtc-ds1307.c,内核配置项为CONFIG_RTC_DRV_DS1307 Location: -> Device Drivers -> Real Tim ...
- zookeeper项目使用几点小结
背景 前段时间学习了zookeeper后,在新的项目中刚好派上了用场,我在项目中主要负责分布式任务调度模块的开发,对我自己来说是个不小的挑战. 分布式的任务调度,技术上我们选择了zookeeper,具 ...
- Juniper SRX 简单命令二
--------------------------Juniper SRX 用户管理--------------------------- Juniper的命令,其实是比较形象的,英文稍微好一点,基本 ...
- 解决"此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站"的问题
在ASP.NET MVC项目中,使用AJAX向控制器发送GET请求获取JSON数据时,出现这个错误:"此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站.若要允许 G ...
- Codeforces Round #414 C. Naming Company
http://codeforces.com/contest/794/problem/C 题意: 有两个人要为公司起名字,每个人手中都有n个字符,现在要取一个n个字符长度的公司名.两人轮流取名,每次选择 ...