【刷题-LeetCode】213. House Robber II
- House Robber II
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.
解 第一家和最后一家不能同时打劫,因此分别去掉第一家和最后一家,计算能够抢到的最大值
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)return 0;
if(nums.size() == 1)return nums[0];
if(nums.size() == 2)return max(nums[0], nums[1]);
return max(rob(nums, 0, nums.size() - 1),rob(nums, 1, nums.size()));
}
int rob(vector<int> &nums, int left, int right){
vector<int>dp(nums.size(), 0);
dp[left] = nums[left];
dp[left+1] = max(nums[left], nums[left + 1]);
for(int i = left + 2; i < right; ++i){
dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
}
return dp[right-1];
}
};
【刷题-LeetCode】213. House Robber II的更多相关文章
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [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 Medium (medium)
原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...
- 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 ...
- LeetCode 213. House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 【刷题-LeetCode】275. H-Index II
H-Index II Given an array of citations sorted in ascending order (each citation is a non-negative in ...
- 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:二叉树下的不能相邻,求能 ...
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
随机推荐
- LuoguP1898 缘分计算 题解
Content 根据一个长度为 \(l\),只含大写字母的字符串算出它的"缘分值". 步骤如下: 给定一个数 \(st\). 将字符串里面的所有字母改成数字(如 A 改成 \(st ...
- LuoguP7714 「EZEC-10」排列排序 题解
Content 给定一个 \(1\sim n\) 的一个排列 \(p\),你每次可以选择一个区间 \([l,r]\) 并花费 \(r-l+1\) 的代价将下标在这个区间内的所有数升序排序,求使得排列 ...
- Centos 配置服务器
Centos 配置服务器 (配置服务器 除了Git Bash Here 还可以安装Xshell 网址:https://xshell.en.softonic.com/ Wincp 网址:https: ...
- JAVA验证手机号码是否正确
PhoneUtils.java package com.common.util; import java.util.regex.Matcher; import java.util.regex.Patt ...
- js-fn函数返回一个引用变量的细节
1.直接返回一个字面量对象 function fun1() { return { age:10, name:'adain' } a = fun1(); b = fun1(); b.age = 18 ; ...
- 【九度OJ】题目1187:最小年龄的3个职工 解题报告
[九度OJ]题目1187:最小年龄的3个职工 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1187 题目描述: 职工有职工号,姓名, ...
- 【LeetCode】241. Different Ways to Add Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归构建所有表达式 方法二:分而治之 日期 ...
- mac学习Python第二天:开发工具安装、编程方式、中文编码、syntaxError语法错误、注释、语法格式
一.python集成开发工具Visual Studio Code安装配置 1.官网下载安装VSCode 官网地址 https://code.visualstudio.com/下载软件包 VSCode ...
- Codeforces 888C: K-Dominant Character(水题)
You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff ...
- A pure L1-norm principal component analysis
@ 目录 问题 细节 的损失函数 算法 投影 坐标系 载荷向量 A pure L1-norm principal component analysis 虽然没有完全弄清楚其中的数学内涵,但是觉得有趣, ...