213. House Robber II(动态规划)

时间:2021-06-24 23:12:25
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&period; House Robber&comma;213&period; House Robber II

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

  2. leetcode 198&period; House Robber 、 213&period; House Robber II 、337&period; House Robber III 、256&period; Paint House&lpar;lintcode 515&rpar; 、265&period; Paint House II&lpar;lintcode 516&rpar; 、276&period; Paint Fence&lpar;lintcode 514&rpar;

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  3. 【LeetCode】213&period; House Robber II

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

  4. 动态规划 - 213&period; House Robber II

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

  5. &lbrack;LeetCode&rsqb; 213&period; House Robber II 打家劫舍之二

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

  6. &lbrack;LeetCode&rsqb; 213&period; House Robber II 打家劫舍 II

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

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

  8. 213&period; House Robber II

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

  9. LeetCode 213&period; House Robber II

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

随机推荐

  1. iOS8沙盒路径的变化

    iOS8中的的沙盒路径发生了变化 之前是这样的路径,通过NSHomedictionary()获取的家路径 /Users/wupeng/Library/Application Support/iPhon ...

  2. &lbrack;转&rsqb;一个用户SQL慢查询分析,原因及优化

    来源:http://blog.rds.aliyun.com/2014/05/23/%E4%B8%80%E4%B8%AA%E7%94%A8%E6%88%B7sql%E6%85%A2%E6%9F%A5%E ...

  3. &lbrack; MySql学习心得 &rsqb; --Two

    五.MySql 中常用子句 1.where子句 我们都知道在查询数据时,未必会查整个表中的数据,当有条件查询时,就会用到where子句.其结构: select * from  [表名]  where ...

  4. 浅析for in 和for的区别

    区别一: for in是javascript 1.0 中发布的. for each in是作为E4X标准的一部分在javascript 1.6中发布的,而它不是ECMAScript标准的一部分. 这将 ...

  5. unity3D技术之事件函数的执行顺序&lbrack;转&rsqb;

    unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703   在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...

  6. --hdu 1114 Piggy-Bank&lpar;完全背包&rpar;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 AC code: #include<bits/stdc++.h> using nam ...

  7. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  8. Mac Vim 如何设置高亮

    首先进入如下目录 cd /usr/share/vim 然后打开vimrc sudo vim vimrc 在vimrc中的“set backspace=2”这行下插入如下代码: set ai &quot ...

  9. SQLSERVER备份恢复后权限问题简单处理&period;

    1. 同事的服务器出现无法访问表, 应用连不上数据库... 远程了下 发现. 使用业务用户登录数据库之后查询无法下拉帮助到表, 必须增加schemas才可以访问到具体的表. 2. 问题解决. 1. 修 ...

  10. 小程序 login

    app.json : 配置文件 => 文件路径 pages .配置窗口 window.底部导航 tabBar .请求超时时间 networkTimeout app.js : 请求路口文件 wx. ...