213. House Robber II(动态规划)
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.
Example 1:
Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
这个地方的所有房屋都排成一个圆圈。这意味着第一栋房屋是最后一栋房屋的邻居。
思路:首尾算邻居,所以我们分别去掉头,分别去掉尾,然后利用第一问的程序,得到最大偷盗金额。取max.
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
if(n==) return nums[];
vector<int> nums1(nums.begin(),nums.end()-);
vector<int> nums2(nums.begin()+,nums.end());
int m1 = rob1(nums1);
int m2 = rob1(nums2);
return std::max(m1,m2);
}
int rob1(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
if(n==) return nums[];
if(n==) return std::max(nums[],nums[]);
vector<int> dp(n,);
dp[] = nums[];
dp[] = std::max(nums[],nums[]);
for(int i = ;i<n;i++)
dp[i] = std::max(dp[i-],dp[i-]+nums[i]);
return dp[n-];
}
};
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
if(n==) return nums[];
int temp = nums[n-];
nums.pop_back();
int m1 = rob1(nums); nums.push_back(temp);
nums.erase(nums.begin()); int m2 = rob1(nums);
return std::max(m1,m2);
}
int rob1(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
if(n==) return nums[];
if(n==) return std::max(nums[],nums[]);
vector<int> dp(n,);
dp[] = nums[];
dp[] = std::max(nums[],nums[]);
for(int i = ;i<n;i++)
dp[i] = std::max(dp[i-],dp[i-]+nums[i]);
return dp[n-];
}
};
213. House Robber II(动态规划)的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 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】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 - 213. House Robber II
URL: https://leetcode.com/problems/house-robber-ii/ You are a professional robber planning to rob ho ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- Java for LeetCode 213 House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 213. House Robber II
题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
随机推荐
- C++ 非常量引用无效
/* 非常量引用无效 */ #include <iostream> using namespace std; /* C++标准的规定:非常量的引用不能指向临时对象: 为了防止给常量或临时变 ...
- 恒生UFX接口引用计数心得
本文介绍在基于恒生T2SDK基础上开发对接UFX柜台时,有关引用计数的一些心得体会. 下面以配置接口和连接接口为例子来介绍,下面是文档介绍: 创建配置接口说明: 3.1.2 创建配置接口(NewCon ...
- linux下的抓包工具tcpdump
1.由netstat查看网络情况,引出的TCP建立连接.终止连接过程,以及TCP状态分析: 2.Soap=XML+HTTP引出的HTTP协议分析: 3.Soap(Simple Object Acces ...
- Angularjs 学习笔记
Angularjs 表单验证:https://www.w3xue.com/jsjq/angularjs/angularjs-validation.html https://www.cnblogs.co ...
- Zend Optimizer,Zend Guard Loader 和 Zend Opcache 三者之间的区别
PHP的加速插件有三个:Zend Optimizer.Zend Guard Loader 和 Zend Opcache.但其实都是一个,针对不通的php版本.名字叫法不一样而已. Zend Optim ...
- 卸载ie
今天卸载ie11失败,最后使用下面这个命令实现了卸载,记录下 IE11卸载命令如下: FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Wind ...
- 【Static Program Analysis - Chapter 2】 代码的表征之控制流图
(a) an if-then-else (b) a while loop (c) a natural loop with two exits, e.g. while with an if...br ...
- Android学习:Notification状态栏通知
Notification是显示在手机状态栏的通知,它代表一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification.在小米手机上,手指在屏幕顶端向下划 ...
- Spring Security默认的用户登录表单 页面源代码
Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...
- StreamSocket
转载自:http://blog.csdn.net/yuanguozhengjust/article/details/19175085 StreamSocket的基本流程和一般的Socket通信操作类似 ...