House Robber & House Robber II
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.
- 动态规划,设置maxV[i]表示到第i个房子位置,最大收益。
- 递推关系为maxV[i] = max(maxV[i-2]+num[i], maxV[i-1])
class Solution {
public:
int rob(vector<int> &num) {
int n = num.size();
if(n == )
return ;
else if(n == )
return num[];
else
{
vector<int> maxV(n, );
maxV[] = num[];
maxV[] = max(num[], num[]);
for(int i = ; i < n; i ++)
maxV[i] = max(maxV[i-]+num[i], maxV[i-]);
return maxV[n-];
}
}
};
- 用A[0]表示没有rob当前house的最大money,A[1]表示rob了当前house的最大money,那么A[0] 等于rob或者没有rob上一次house的最大值
- 即A[i+1][0] = max(A[i][0], A[i][1]).. 那么rob当前的house,只能等于上次没有rob的+money[i+1], 则A[i+1][1] = A[i][0]+money[i+1]. 只需要两个变量保存结果就可以了
int max(int a, int b)
{
if(a > b)
return a;
else
return b;
}
int rob(int* nums, int numsSize) {
int best0 = ; // 表示没有选择当前houses
int best1 = ; // 表示选择了当前houses
for(int i = ; i < numsSize; i++){
int temp = best0;
best0 = max(best0, best1); // 没有选择当前houses,那么它等于上次选择了或没选择的最大值
best1 = temp + nums[i]; // 选择了当前houses,值只能等于上次没选择的+当前houses的money
}
return max(best0, best1);
}
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.
- 把第一个房子,作为例外处理
- 如果选择第一个房子,则从第三个房子开始到倒数第二房子,属于常规
- 如果不选第一个房子,则从第二个房子开始属于常规
int rob(int* nums, int numsSize) {
if(numsSize == )
return ;
//分成第一家抢和不抢
//如果第一家不抢,最后一个就不用考虑抢不抢
int hos11 = nums[];
int hos10 = ;
int best1 = ; //rub cur house
int best0 = ; //not rub cur house
for(int i = ; i < numsSize - ; i++){
int tmp = best0;
best0 = best1 > best0 ? best1 : best0;
best1 = tmp + nums[i];
}
hos11 += best1 > best0 ? best1 : best0;
//如果第一家抢,最后一家不能抢,返回倒数第二家的情况
best1 = ;
best0 = ;
for(int i = ; i < numsSize; i++){
int tmp = best0;
best0 = best1 > best0 ? best1 : best0;
best1 = tmp + nums[i];
}
hos10 = best1 > best0 ? best1 : best0;
return hos11 > hos10 ? hos11 : hos10;
}
House Robber & House Robber II的更多相关文章
- LeetCode之“动态规划”:House Robber && House Robber II
House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...
- 【leetcode】House Robber & House Robber II(middle)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 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 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- [LeetCode]House Robber II (二次dp)
213. House Robber II Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...
- House Robber I & II & III
House Robber You are a professional robber planning to rob houses along a street. Each house has a c ...
随机推荐
- JQ对JSON的增删改
var userlist={ }, } } //方法一 userlist.row1.sex="女";//添加 userlist.row3={name:};//添加 userlist ...
- NSURLSessionUploadTask 上传
-(void)uploadPhotoImageWithUrl:(NSString*)url userIMID:(NSString*)userIMID imageData:(NSData*)imageD ...
- DOM常见属性及用法
1:innerHTML.outerHTML.innerText.outerText innerHTML: 设置或获取位于对象起始和结束标签内的HTML. outerHTML: 设置或获取对象及其内容的 ...
- FileUpload控件
FileUpload控件 属性:FileName: 获取上传的文件名 HasFile: 是否选择(存在)上传的文件 ContentLength: 获得上窜文件的大小,单位是字节(byte) 方法:Se ...
- console.log几个小知识
<script> //百度的console console.log('一张网页,要经历怎样的过程,才能抵达用户面前?\n一位新人,要经历怎样的成长,才能站在技术之巅?\n探寻这里的秘密:\ ...
- php cli模式没有加载php.ini
这两天在虚拟机的linux里编译安装了php,同时也把swoole的扩展也编译上了.在/etc/php.ini里加上了extension=swoole.so,但是用php -m 查看加载的模块并没有s ...
- Hibernate防止SQL注入
如果在查询字段中输入单引号"'",则会报错,这是因为输入的单引号和其他的sql组合在一起编程了一个新的sql,实际上这就是SQL注入漏洞,后来我在前台和后台都对输入的字符进行了判断 ...
- Flex中如何通过horizontalTickAligned和verticalTickAligned样式指定线图LineChart横竖方向轴心标记的例子
原文http://blog.minidx.com/2008/12/03/1669.html 接下来的例子演示了Flex中如何通过horizontalTickAligned和verticalTickAl ...
- SQL Server带游标的SQL
DECLARE test_cursor CURSOR FOR SELECT ID FROM dbo.T_BD_Restaurant WHERE id <> '0AAB2E55-79F8-4 ...
- Spring、编码剖析Spring管理Bean的原理
引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...