【刷题-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 ...
随机推荐
- DKT模型及其TensorFlow实现(Deep knowledge tracing with Tensorflow)
今年2月15日,谷歌举办了首届TensorFlow Dev Summit,并且发布了TensorFlow 1.0 正式版. 3月18号,上海的谷歌开发者社区(GDG)组织了针对峰会的专场回顾活动.本文 ...
- 【剑指Offer】左旋转字符串 解题报告(Python)
[剑指Offer]左旋转字符串 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目 ...
- matplotlib 进阶之Artist tutorial(如何操作Atrist和定制)
目录 基本 plt.figure() fig.add_axes() ax.lines set_xlabel 一个完整的例子 定制你的对象 obj.set(alpha=0.5, zorder=2), o ...
- 初识JavaScript变量
一.什么是变量? 变量即变化的量,在JS中变量是松散类型的,可以用来保存任何数据类型.把数据取个名字,放在内存中,就称之为变量! 通过变量名可以取到对应数据 二.为什么使用变量? 程序:代码的集合,一 ...
- Java——HashMap集合详解
第一章 HashMap集合简介 1.1 介绍 HashMap基于哈希表的Map接口实现,是以key-value存储形式存在,即主要用来存放键值对.HashMap 的实现不是同步的,这意味着它不是线程安 ...
- JavaScript交互式网页设计作业目录(作业笔记)
JavaScript交互式网页设计笔记 • [目录] 我的大学笔记>>> 第1章 JavaScript基本语法>>> 1.1.4 使用 JavaScript 的 H ...
- 安装rebar3
下载编译好的版本 wget https://s3.amazonaws.com/rebar3/rebar3 chmod +x /home/hylink/rebar3 (赋权) ./rebar3 loca ...
- Lombok 安装配置及使用方法
pom.xml 引入依赖 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <!--Feb 0 ...
- [学习笔记] SpringBoot 之 Helloworld
创建项目 IDEA / File / New / Project / Spring Initalizr / Next Group: com.wu 公司官网域名反写 Artifact: hellowor ...
- css 基础 css引入方式
color:red; //改变颜色 font-size:18px : //改变文字大小 background-color : blue; //设置背景颜色 width:100px ://设置宽度 he ...