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.

Example

Given [3, 8, 4], return 8.
Challenge

O(n) time and O(1) memory.

LeetCode上的原题,请参见我之前的博客House Robber

解法一:

class Solution {
public:
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> A) {
if (A.size() <= ) return A.empty() ? : A[];
vector<long long> dp{A[], max(A[], A[])};
for (int i = ; i < A.size(); ++i) {
dp.push_back(max(dp[i - ] + A[i], dp[i - ]));
}
return dp.back();
}
};

解法二:

class Solution {
public:
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> A) {
long long a = , b = ;
for (int i = ; i < A.size(); ++i) {
if (i % == ) {
a += A[i];
a = max(a, b);
} else {
b += A[i];
b = max(a, b);
}
}
return max(a, b);
}
};

解法三:

class Solution {
public:
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> A) {
long long a = , b = ;
for (int i = ; i < A.size(); ++i) {
long long m = a, n = b;
a = n + A[i];
b = max(m, n);
}
return max(a, b);
}
};

[LintCode] House Robber 打家劫舍的更多相关文章

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

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

  2. [LintCode] House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  3. [LeetCode] House Robber 打家劫舍

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

  4. [LeetCode] 198. House Robber 打家劫舍

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

  5. 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...

  6. 198 House Robber 打家劫舍

    你是一个专业的强盗,计划抢劫沿街的房屋.每间房都藏有一定的现金,阻止你抢劫他们的唯一的制约因素就是相邻的房屋有保安系统连接,如果两间相邻的房屋在同一晚上被闯入,它会自动联系警方.给定一个代表每个房屋的 ...

  7. [LeetCode] 213. House Robber II 打家劫舍 II

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

  8. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  9. [LeetCode] 656. Coin Path 硬币路径

    Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...

随机推荐

  1. 无法打开包括文件:“windows.h”: No such file or directory

      VS2012 出现如下错误: 无法打开包括文件:"windows.h": No such file or directory   解决办法,将 C:\Program Files ...

  2. C语言里面捕获错误机制

    在C语言中异常处理一般有这么几种方式: 1.使用标准C库提供了abort()和exit()两个函数,它们可以强行终止程序的运行,其声明处于<stdlib.h>头文件中. 2.使用asser ...

  3. Java8中的default方法

    default方法 Java 8中引入了一个新的概念,叫做default方法,也可以称为Defender方法,或者虚拟扩展方法(Virtual extension methods). Default方 ...

  4. AxureRP7.0各类交互效果汇总帖(转)

    了便于大家参考,我把这段时间发布分享的所有关于AxureRP7.0的原型做了整理. 以下资源均有对应的RP源文件可以下载. 当然 ,其中有部分是需要通过完成解密游戏[攻略]才能得到下载地址或者下载密码 ...

  5. currentStyle

    用js的style属性可以获得html标签的样式,但是不能获取非行间样式. 解决方法: 在IE下可以用currentStyle; 在FF下用getComputedStyle; 然而,为了让其兼容,解决 ...

  6. SoapUI接口测试之JDBC(三)

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用java语言编写的类和接口 ...

  7. 安卓图表引擎AChartEngine(三) - 示例源码折线图、饼图和柱状图

    折线图: package org.achartengine.chartdemo.demo.chart; import java.util.ArrayList; import java.util.Lis ...

  8. poj2566 尺取法

    题意: 输入 n m  之后输入n个数  之后m个询问  对于每个询问 输入一个t    输出  三个数 ans l r  表示从l 到 r的所有数的和的绝对值最接近t 且输出这个和ans   思路: ...

  9. DSP using MATLAB 示例 Example3.13

    上代码: w = [0:1:500]*pi/500; % freqency between 0 and +pi, [0,pi] axis divided into 501 points. H = ex ...

  10. Swift3.0语言教程使用Unicode范式标准化获取字符串

    Swift3.0语言教程使用Unicode范式标准化获取字符串 Swift3.0语言教程使用Unicode范式标准化获取字符串,在NSString中可以使用4个属性去使用Unicode范式标准化获取字 ...