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 ...
随机推荐
- //NSUserDeafult 图片的保存与读取
//NSUserDeafult保存图片数据到本地 -(void)saveImage:(UIImage *)image{ NSData* data=[NSKeyedArchiver archivedDa ...
- 最强烈推荐-我的java收藏夹(内有国内最好的java论坛)
原地址: http://bbs.chinaitlab.com/dispbbs.asp?boardid=148&id=34276 国内: www.chinajavaworld.com-论坛人很多 ...
- QT皮肤框架-TQUI
本皮肤框架的相关文档,请在附件中下载,包括测试程序源码,帮助文档.相关文档可到我的百度网盘中下载,或者在本贴附件中下载. 百度网盘地址:TQUI-V1.0项目说明及测试程序源码 项目更新说明:---- ...
- (原+转)ubuntu14中结束多个caffe进程中的某个
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5948237.html 参考网址: http://www.2cto.com/os/201407/3215 ...
- 在shell脚本中调用sqlplus
#!/bin/bash sqlplus dc_file_data_js/dc_file_data_js << EOF1 set linesize 500; set pagesize 100 ...
- python运维开发(二十四)----crm权限管理系统
内容目录: 数据库设计 easyUI的使用 数据库设计 权限表Perssion 角色表Role 权限和角色关系表RoleToPermission 用户表UserInfo 用户和角色关系表UserInf ...
- automation studio 6.0 破解版 32位
破解软件在iso文件的patch目录下 链接:http://pan.baidu.com/s/1o8KR7rc 密码:y87g
- STL中,迭代器的分类
五类迭代器如下: 1.输入迭代器:只读,一次传递 为输入迭代器预定义实现只有istream_iterator和istreambuf_iterator,用于从一个输入流istream中读取.一个输 ...
- CentOS7安全设置 yum-cron系统自动更新,firewalld防火墙简单使用
PermitRootLogin nosystemctl restart sshd.service; yum -y install firewalld; systemctl start firewall ...
- 关于 Private strand flush not complete
网友发来告警日志,原本是关于一个死锁的情形,而另外的一个问题则是从redo log buffer写出到redo log file出现了不能分配新的日志,Private strand flush not ...