[LintCode] House Robber 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 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.
Notice
This is an extension of House Robber.
Example
nums = [3,6,4], return 6
LeetCode上的原题,请参见我之前的博客House Robber II。
解法一:
class Solution {
public:
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
int houseRobber2(vector<int>& nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
vector<int> a = nums, b = nums;
a.erase(a.begin()); b.pop_back();
return max(rob(a), rob(b));
}
int rob(vector<int> &nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
vector<int> dp{nums[], max(nums[], nums[])};
for (int i = ; i < nums.size(); ++i) {
dp.push_back(max(dp[i - ] + nums[i], dp[i - ]));
}
return dp.back();
}
};
解法二:
class Solution {
public:
/**
* @param nums: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
int houseRobber2(vector<int>& nums) {
if (nums.size() <= ) return nums.empty() ? : nums[];
return max(rob(nums, , nums.size() - ), rob(nums, , nums.size()));
}
int rob(vector<int> &nums, int left, int right) {
int a = , b = ;
for (int i = left; i < right; ++i) {
int m = a, n = b;
a = n + nums[i];
b = max(m, n);
}
return max(a, b);
}
};
[LintCode] House Robber II 打家劫舍之二的更多相关文章
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LintCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 213 House Robber II 打家劫舍 II
注意事项: 这是 打家劫舍 的延伸.在上次盗窃完一条街道之后,窃贼又转到了一个新的地方,这样他就不会引起太多注意.这一次,这个地方的所有房屋都围成一圈.这意味着第一个房子是最后一个是紧挨着的.同时,这 ...
- [LeetCode] 337. House Robber III 打家劫舍 III
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [LeetCode]House Robber II (二次dp)
213. House Robber II Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
随机推荐
- document.body.scrollTop
标准浏览器:document.documentElement.scrollTop; 谷歌浏览器:document.body.scrollTop; var scrollTop = document.do ...
- SQLServer批量创建有规则的数据
根据需求要生成从kkk001,kkk002开始的100个递增账号 手插要死人的,用SQL脚本轻松完成: declare @a int ) ) begin ) ) end declare:申明变量, @ ...
- Java的静态导入
静态导入作用是可以适当减少代码量,但实际上减少得很有限,实际应用中也用的不多,但是作为Java的特性,我们应该适当了解: //静态导入方法或者常量 import static java.lang.Sy ...
- 【微信Java开发 --1】内网穿透外网,使用外网域名可以访问到本地项目
1.首先上https://natapp.cn/ 2.接下来在网站申请账号 3.购买免费隧道 4.为你的免费隧道设置名称以及端口号,由于本人本地的使用Tomcat做服务器,所以用惯了8080端口,因此设 ...
- jsp include指令
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- CSS3_实现圆角效果box-shadow
1.outline的直角与圆角 来给个div: <div class="use-outline"></div> 来再给个样式: .use-outline{ ...
- WebSocket协议开发
一直以来,网络在很大程度上都是围绕着HTTP的请求/响应模式而构建的.客户端加载一个网页,然后直到用户点击下一页之前,什么都不会发生.在2005年左右,Ajax开始让网络变得更加动态了.但所有的HTT ...
- 安卓微POS-PDA手持终端,支持离线在线联网销售开单;移动开单 盘点 功能
采购单.采购退货单 销售单.销售退货单.收款.优惠.赠品等操作实现盘点作业(多台设备同时作业,相同商品,数量累计) 现场打印票据 实现采购订单.采购单.采购退货单.销售订单.销售单.销售退货单验货没 ...
- 基于jdk1.7实现的excel导出工具类
通用excel导出工具类,基于泛型.反射.hashmap 以及基于泛型.反射.bean两种方式 import java.io.*;import java.lang.reflect.Field;impo ...
- [bzoj1068]压缩[区间动规]
看了lujiaxin的blog,感觉自己好浪啊....好难过 刷题的时候不够投入,每种算法都是只写一两道就过去了,这样怎么可能进步嘛 不要总是抱怨时间太少了 都是自己不努力>_< 好啦 看 ...