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:

  1. Input: [2,3,2]
  2. Output: 3
  3. Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
  4.   because they are adjacent houses.

Example 2:

  1. Input: [1,2,3,1]
  2. Output: 4
  3. Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
  4.   Total amount you can rob = 1 + 3 = 4.

这个地方的所有房屋都排成一个圆圈。这意味着第一栋房屋是最后一栋房屋的邻居。

思路:首尾算邻居,所以我们分别去掉头,分别去掉尾,然后利用第一问的程序,得到最大偷盗金额。取max.

  1. class Solution {
  2. public:
  3. int rob(vector<int>& nums) {
  4. int n = nums.size();
  5. if(n==) return ;
  6. if(n==) return nums[];
  7. vector<int> nums1(nums.begin(),nums.end()-);
  8. vector<int> nums2(nums.begin()+,nums.end());
  9. int m1 = rob1(nums1);
  10. int m2 = rob1(nums2);
  11. return std::max(m1,m2);
  12. }
  13. int rob1(vector<int>& nums) {
  14. int n = nums.size();
  15. if(n==) return ;
  16. if(n==) return nums[];
  17. if(n==) return std::max(nums[],nums[]);
  18. vector<int> dp(n,);
  19. dp[] = nums[];
  20. dp[] = std::max(nums[],nums[]);
  21. for(int i = ;i<n;i++)
  22. dp[i] = std::max(dp[i-],dp[i-]+nums[i]);
  23. return dp[n-];
  24. }
  25. };
  1. class Solution {
  2. public:
  3. int rob(vector<int>& nums) {
  4. int n = nums.size();
  5. if(n==) return ;
  6. if(n==) return nums[];
  7. int temp = nums[n-];
  8. nums.pop_back();
  9. int m1 = rob1(nums);
  10.  
  11. nums.push_back(temp);
  12. nums.erase(nums.begin());
  13.  
  14. int m2 = rob1(nums);
  15. return std::max(m1,m2);
  16. }
  17. int rob1(vector<int>& nums) {
  18. int n = nums.size();
  19. if(n==) return ;
  20. if(n==) return nums[];
  21. if(n==) return std::max(nums[],nums[]);
  22. vector<int> dp(n,);
  23. dp[] = nums[];
  24. dp[] = std::max(nums[],nums[]);
  25. for(int i = ;i<n;i++)
  26. dp[i] = std::max(dp[i-],dp[i-]+nums[i]);
  27. return dp[n-];
  28. }
  29. };

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. BarTender中如何调整数据输入表单的大小?

    BarTender中的表单设计,是一个简单而又复杂的操作.简单的是它提供很多实用的工具,帮助用户实现更多的功能,复杂的是要对其进行排版设计,这就要看小伙伴们的个人要求高低了. 自定义数据输入表单时,你 ...

  2. 目前(2018年)在北京java程序员平均薪水是多少呢?

    1. 这个要看看个人java开发能力,你那个自己带项目做团队的比较高哦 2. 一般来说刚毕业的本科实习生大约在5000左右,干半年基本都张到7.5左右了. 3. Java程序员一般都集中在北京,上海和 ...

  3. TensorFlow 1.4利用Keras+Estimator API进行训练和预测

    Tensorflow 1.4中,Keras作为作为核心模块可以直接通过tf.keas进行调用,但是考虑到keras对tfrecords文件进行操作比较麻烦,而将keras模型转成tensorflow中 ...

  4. 《转载》RPC入门总结(一)RPC定义和原理

    转载:深入浅出 RPC - 浅出篇 转载:RPC框架与Dubbo完整使用 转载:深入浅出 RPC - 深入篇 转载:远程调用服务(RPC)和消息队列(Message Queue)对比及其适用/不适用场 ...

  5. Delphi过程函数传递参数的几种方式

    Delphi过程函数传递参数的几种方式  在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out. 另一种不加修饰符的为默认按值传递参数. 一.默认方式以值方式传递参数 proced ...

  6. [原]openstack-kilo--issue(二十三)虚拟机状态错误power_status为shutdonw或者vm_status为error

    问题点:虚拟机由于存储不足出现了错误标识 主要显示为状态错误: 1)  vm_status 显示为 error 2) power_status 显示为 shutdown 解决方案: 更改表 nova. ...

  7. Cookiecutter: 更好的项目模板工具:(2)安装及基础使用

    安装 通过python包管理工具 命令行输入 $pip install cookiecutter 或者 # mac os经常会禁止用户全局安装python包 $pip install --user c ...

  8. 如何判断java对象是否为String数组

    if (entry.getValue() instanceof String[]) {// ko .................... }

  9. pandas replace 替换功能function

    list like replace method dict like replace method regex expression import pandas as pd import numpy ...

  10. 网络流24T

    说出来你们可能不信,我咕了三个多星期了,今晚忽然不想再写题了,(写自闭了,把这边整理一下 1. 洛谷P2756 飞行员配对问题 二分图匹配: #include <bits/stdc++.h> ...