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. Effective C++ 之 Item 2:尽量以 const, enum, inline 替换 #define

    Effective C++ Chapter 1. 让自己习惯C++(Accustoming Yourself to C++) Item 2. 尽量以 const, enum, inline 替换 #d ...

  2. Vue入门笔记#数据绑定语法

    #数据绑定语法# #文本: 数据绑定的基础表型形式,使用“Mustache”语法(双大括号)(特意查了一下Mustache同“moustache”释义为:髭:上唇的胡子,小胡子,最起码我觉得挺形象的) ...

  3. 2016.9.13 JavaScript入门之六基础函数

    1.Math.random()函数: 2.math.floor(x)返回小于参数x的最大整数,即对浮点数向下取整. 例如:random本身只产生(0~1)之间的小数,random()*10 意思是产生 ...

  4. SU suxcontour命令学习

  5. 使用recon/domains-hosts/baidu_site模块,枚举baidu网站的子域

    使用recon/domains-hosts/baidu_site模块,枚举baidu网站的子域 [实例3-1]使用recon/domains-hosts/baidu_site模块,枚举baidu网站的 ...

  6. css -- 布局元素

    默认情况下拥有布局的元素:HTML ,table,tr,td,img,hr,input,select,textarea,button,iframe,embed,object,applet,marque ...

  7. node body-parser

    var express = require('express'); var app = express(); var bodyParser = require('body-parser'); // p ...

  8. iOS LoginDemo

    // // ViewController.m // FicowLoginDemo1 // // Created by Ficow on 15/11/12. // Copyright © 2015年 F ...

  9. Bower In ASP.NET Core

    创建一个ASP.NET Core MVC项目的时候,会产生一个bower.json的文件,用于管理前段的js. NPM & Bower NPM主要运用于Node.js项目的内部依赖包管理,安装 ...

  10. #ASP.NET Core 1.0 Key Features

    Cross platform support and flexible runtime engine(跨平台支持和灵活的运行时引擎) ASP.NET Core 1.0 offers support f ...