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的更多相关文章

  1. LeetCode之“动态规划”:House Robber && House Robber II

    House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...

  2. 【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 ...

  3. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  4. [LeetCode] 213. House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  5. LeetCode House Robber III

    原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...

  6. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  7. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  8. [LeetCode]House Robber II (二次dp)

    213. House Robber II     Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...

  9. House Robber I & II & III

    House Robber You are a professional robber planning to rob houses along a street. Each house has a c ...

随机推荐

  1. hdu1814 Peaceful Commission,2-sat

    题目大意:一国有n个党派.每一个党派在议会中都有2个代表,现要组建和平委员会,要从每一个党派在议会的代表中选出1人,一共n人组成和平委员会.已知有一些代表之间存在仇恨,也就是说他们不能同一时候被选为和 ...

  2. 移动web开发前准备知识了解(html5、jquery)笔记

    1.经常使用 插件工具  chrome插件:   Mobile & Tablet Emulator(用于常见移动端适配):(重点) Mobile Emulator is an useful o ...

  3. WCF学习心得

    之前很经常听说WCF,不过没有怎么接触过,直到最近才真正使用到WCF,虽然也只是皮毛而已,在此也做个记录总结吧. 下图是我使用WCF的练手项目,由于是使用VS2010直接创建的WCF服务应用程序,VS ...

  4. 对js中prototype的理解

    一直不理解child.prototype = new Parent()和child.prototype =Parent.prototype的区别,到现在为止,我觉得它俩最大的区别就是:前者共享构造器里 ...

  5. 关于js对象值的传递

    结合红宝书和网上的一些文章,记录下自己对关于js对象的值的传递的一些理解. js对象是保存在堆内存中的,当把对象赋值给变量时,是把对象在堆内存的引用(地址)赋值给了变量,变量通过地址来访问对象.下面来 ...

  6. RDLC报表系列(五) 简单的图表-柱状图

    继续接上一篇的内容,本文主要是讲图标的内容,本文就是简单的图标,复杂的柱状图和折线图在下一文章中介绍. 数据源还是上文RDLC报表系列(四) 矩阵中的相同 1.还是继续使用demo2的文件

  7. SSH框架常会出现的一些错误

    1.jquery datatable插件报JSON数据错误 错误原因:hql语句拼接有问题,前一个字符串与后一个字符串之间缺少空格,导致数据库查询失败. 2.addInput页面中input内容不为空 ...

  8. C#修改 Excel指定单元格的值

    /// <summary> /// 将指定字符串写入指定单元格中 /// </summary> /// <param name="data">要 ...

  9. ASP.NET 生命周期(原文翻译)

    在网上看到这篇文章,老外写的,里面很多图片挺精致,顺带翻译过来给大家分享下,英语太次好多地方都翻不过来 ASP.NET application and page life cycle Download ...

  10. [stack]Evaluate Reverse Polish Notation

    Total Accepted: 55722 Total Submissions: 249668 Difficulty: Medium Evaluate the value of an arithmet ...