- 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 ...
随机推荐
-
asp.net mvc4 使用 System.Web.Optimization 对javascript和style的引入、代码合并和压缩的优化(ScriptBundle,StyleBundle,Bundling and Minification )
Bundling and Minification两个单词对今天的内容有个比较好的总结. 问题所在 一. 在asp.net包括mvc项目中,引入js和css也许有人认为是个很容易和很简单操作的事情,v ...
-
php一些特殊函数的使用实例详解
<?php /* * PHP Array 函数大全 * * array() 创建数组. 3 array_change_key_case() 返回其键均为大写或小写的数组. 4 array_chu ...
-
Zabbix3.0+CentOS7.0+MariaDB5.5监视服务器安装
本次安装采用: Centos7.0 Zabbix3.0 MariaDB5.5 ------------------- 2012/12/2更新 最新的Centos7.1或者Redhat7.1版本在最后 ...
-
Node.js 学习(三) NPM 使用介绍
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...
-
process launch failed : failed to get the task for process xxx
原因: 证书问题,project和targets的证书都必须是开发证书,ADHOC的证书会出现此问题. 解决方案: project和targets的证书使用开发证书. 其他: This error ...
-
【Java学习笔记之二十四】对Java多态性的一点理解
面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...
-
leecode第二百零六题(反转链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
-
golang环境 centos 7
https://blog.csdn.net/ggq89/article/details/82682171 Linux下Go的安装.配置 .升级和卸载 https://blog.csdn.net/we ...
-
20155230 2016-2017-2 《Java程序设计》第七周学习总结
20155230 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 世界时:在1972年引入UTC之前,GMT与UT是相同的 格林威治标准时间(GMT),现已不 ...
-
同步ajax请求
/* * 发送同步ajax请求的函数 CreateBy 秋水 */ function syncAjax(data) { var resp = null; $.ajax({ type : "P ...