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 ...
随机推荐
- BarTender中如何调整数据输入表单的大小?
BarTender中的表单设计,是一个简单而又复杂的操作.简单的是它提供很多实用的工具,帮助用户实现更多的功能,复杂的是要对其进行排版设计,这就要看小伙伴们的个人要求高低了. 自定义数据输入表单时,你 ...
- 目前(2018年)在北京java程序员平均薪水是多少呢?
1. 这个要看看个人java开发能力,你那个自己带项目做团队的比较高哦 2. 一般来说刚毕业的本科实习生大约在5000左右,干半年基本都张到7.5左右了. 3. Java程序员一般都集中在北京,上海和 ...
- TensorFlow 1.4利用Keras+Estimator API进行训练和预测
Tensorflow 1.4中,Keras作为作为核心模块可以直接通过tf.keas进行调用,但是考虑到keras对tfrecords文件进行操作比较麻烦,而将keras模型转成tensorflow中 ...
- 《转载》RPC入门总结(一)RPC定义和原理
转载:深入浅出 RPC - 浅出篇 转载:RPC框架与Dubbo完整使用 转载:深入浅出 RPC - 深入篇 转载:远程调用服务(RPC)和消息队列(Message Queue)对比及其适用/不适用场 ...
- Delphi过程函数传递参数的几种方式
Delphi过程函数传递参数的几种方式 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out. 另一种不加修饰符的为默认按值传递参数. 一.默认方式以值方式传递参数 proced ...
- [原]openstack-kilo--issue(二十三)虚拟机状态错误power_status为shutdonw或者vm_status为error
问题点:虚拟机由于存储不足出现了错误标识 主要显示为状态错误: 1) vm_status 显示为 error 2) power_status 显示为 shutdown 解决方案: 更改表 nova. ...
- Cookiecutter: 更好的项目模板工具:(2)安装及基础使用
安装 通过python包管理工具 命令行输入 $pip install cookiecutter 或者 # mac os经常会禁止用户全局安装python包 $pip install --user c ...
- 如何判断java对象是否为String数组
if (entry.getValue() instanceof String[]) {// ko .................... }
- pandas replace 替换功能function
list like replace method dict like replace method regex expression import pandas as pd import numpy ...
- 网络流24T
说出来你们可能不信,我咕了三个多星期了,今晚忽然不想再写题了,(写自闭了,把这边整理一下 1. 洛谷P2756 飞行员配对问题 二分图匹配: #include <bits/stdc++.h> ...