You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

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(动态规划)的更多相关文章

  1. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  2. 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:二叉树下的不能相邻,求能 ...

  3. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  4. 【刷题-LeetCode】213. House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

  5. 动态规划 - 213. House Robber II

    URL: https://leetcode.com/problems/house-robber-ii/ You are a professional robber planning to rob ho ...

  6. [LeetCode] 213. House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  7. [LeetCode] 213. House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  8. 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 ...

  9. 213. House Robber II

    题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...

随机推荐

  1. Ubuntu 16.04设置开机启动脚本的方法

    需求:公司卡片机容量太小,只有100G,由于使用的人比较的多,开机使用后有时候就会出现磁盘空间占满数据写不进去的情况,影响工作进度,而且每次使用完都得关掉卡片机,所以就有必要写个清理磁盘的脚本,当卡片 ...

  2. 关于JVM内存的N个问题

    JVM的内存区域是怎么划分的? JVM的内存划分中,有部分区域是线程私有的,有部分是属于整个JVM进程:有些区域会抛出OOM异常,有些则不会,了解JVM的内存区域划分以及特征,是定位线上内存问题的基础 ...

  3. Vue之初识Vue

    前言 如果你之前已经习惯了用jQuery操作DOM,学习Vue.js时请先抛开手动操作DOM的思维, 因为Vue.js是数据驱动的,你无需手动操作DOM.它通过一些特殊的HTML语法,将DOM和 数据 ...

  4. sprintf的Bug

    ]; sprintf(buffer,,,); 这样一般不崩溃,但是10次,有那么一次会崩溃 我只能说后面改成 0.0就可以了...

  5. Android用户点击返回按钮两次退出整个APP

    最近的APP项目有一个需求就是连续点击两次返回按钮,退出整个APP,而不是返回到上一个页面,这个连续是有时间限制的,在我的项目里,我设置成2秒钟,如果两秒之内点击了两次,就代表用户想要退出整个APP, ...

  6. babel配置项目目录支持转换es6语法,引入非项目目录js后,引入Js转换无效

    我遇到了一个问题,我在已经配置babel的项目中通过require引入了一个项目目录外层的另一个js文件,前期是可以成功转换并打包的,但是到了后期就不行了,报错: 这个报错的意思是,引入的js文件中有 ...

  7. 股票配资源码系统APP股票配资系统PC版配资系统

    股票配资策略系统一套,pc+wap双端,封装app! 需要服务器环境: LNMP/LAMP ,域名,短信服务,IOS端APP需要企业签名发布,或者有金融行业资质到APPstore发布 产品介绍: 全套 ...

  8. Android Runtime Stats

    Android 在 API 23 增加了运行时 GC 状态的获取接口,用法如下: Map<String, String> map = Debug.getRuntimeStats(); St ...

  9. mac 初次配置apache,及mac下安装mysql

    先打开apache,在浏览器上输入  localhost     回车后会如果屏幕上显示:It works! 如下图: 这说明你的apache已开启 mac 下apache配置(mac自带apache ...

  10. Coffee and Coursework (Easy version)

    Coffee and Coursework (Easy version) time limit per test 1 second memory limit per test 256 megabyte ...