198. House Robber,213. House Robber II
198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that 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.
class Solution {
public:
int rob(vector<int>& nums) {
int nums_size = nums.size();
int max_money = ; vector<int>dp(nums_size,); for(int i=;i<nums_size;++i){
int m = ;
for(int j=i-;j>=;j--){
m = max(m,dp[j]);
}
dp[i] = m+nums[i];
max_money = max(max_money,dp[i]);
}
return max_money;
} }; /**
dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
[9,8,9,20,8]
[1,2,3,55,54,2]
*/
213. House Robber II
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
/**
dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
[9,8,9,20,8]
[1,2,3,55,54,2]
*/
class Solution {
public:
int rob(vector<int>& nums,int start,int end) {
if(end<=start) return ;
int nums_size = end-start;
int max_money = ; vector<int>dp(nums_size,);
int k = ;
cout<<"start="<<start<<" end="<<endl;
for(int i=start;i<end;++i){
int m = ;
for(int j=k-;j>=;j--){
m = max(m,dp[j]);
}
dp[k] = m+nums[i];
cout<<"dp["<<(k)<<"]="<<dp[k]<<endl;
max_money = max(max_money,dp[k]);
k++;
} return max_money;
}
int rob(vector<int>& nums) {
int nums_size = nums.size();
return nums_size== ? nums[] : max(rob(nums,,nums_size-),rob(nums,,nums_size));
}
};
198. House Robber,213. House Robber II的更多相关文章
- 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 打家劫舍之二
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 ...
- 【LeetCode】213. House Robber II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house 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 ...
- LeetCode 213. House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
随机推荐
- C学习笔记 - 指针
指针与数组 ,,,,}; int *p; p = a; printf("*a = %d\n",*a); printf("*p = %d\n",*p); prin ...
- C#线程池ThreadPool的理解
在多线程编程中,线程的创建和销毁是非常消耗系统资源的,因此,C#引入了池的概念,类似的还有数据库连接池,这样,维护一个池,池内维护的一些线程,需要的时候从池中取出来,不需要的时候放回去,这样就避免了重 ...
- Webform Lable
- jira汉化
https://translations.atlassian.com/dashboard/dashboard 下载汉化jar文件,在jira中上传插件,系统设置中文即可 LOFTER:我们的故事 ...
- project euler 26:Reciprocal cycles
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with d ...
- pubwin会员合并
此博文已移至爬不稳独立博客:www.pubwin2009.net连接:http://www.pubwin2009.net/index.php/post/15.html 我们说下过程(这里,我们要求两个 ...
- 验证abc三列数字符合我的小弟要求
需求好像是: 1.第一列数据有重复的找出来,并且找出它的重复位置 2.第三列根据第一列得出的位置,取出相应位置的数据进行相加 3.相加的结果 是否等同于第二列的对应位置数据 <!DOCTYPE ...
- 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】
正愁如何选择构建项目中的视图呢,现在官方推荐画板 Storybord...但是好像 xib貌似更胜一筹.以前的老棒子总喜欢装吊,用代码写....用代码堆一个HTML页面不知道你们尝试过没有.等页面做出 ...
- apache+php+mysql常见集成环境安装包
http://www.thinksaas.cn/group/topic/33/ apache+php+mysql是常见php环境,在windows下也称为WAMP,对于初学者自选版本搭建总是会遇到一些 ...
- 剑指offer-面试题1:赋值运算符函数
题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数 class CMyString { public: CMyString(char *pData=NULL); CMyString ...