[LintCode] 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.
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 打家劫舍的更多相关文章
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- [LintCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...
- 198 House Robber 打家劫舍
你是一个专业的强盗,计划抢劫沿街的房屋.每间房都藏有一定的现金,阻止你抢劫他们的唯一的制约因素就是相邻的房屋有保安系统连接,如果两间相邻的房屋在同一晚上被闯入,它会自动联系警方.给定一个代表每个房屋的 ...
- [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] 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 ...
- [LeetCode] 656. Coin Path 硬币路径
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
随机推荐
- leetcode1237
date: 2015-09-09 20:20:58 Two Sum Given an array of integers, find two numbers such that they add up ...
- Eclipse+CDT+GDB调试android NDK程序(转)
Eclipse+CDT+gdb调试android ndk程序 先介绍一下开发环境,在这个环境下,up主保证是没有问题的. ubuntu 11.10 eclipse 3.7(indego) for ja ...
- Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构
分享两篇Win 10应用开发的XML文档结构:Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构. Win 10 开发中Adapt ...
- python 简单的txt文件读写
1 读取txt文件.跟c相比,python的文件读写简直是方便的可怕 首先是读取文件 首先获得文件名称,然后通过 open函数打开文件,通过for循环逐行读出文件内容 #!python file by ...
- HDU 3374 最小/大表示法+KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给定一个串s,该串有strlen(s)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...
- [转] FastMM、FastCode、FastMove的使用
http://blog.csdn.net/akof1314/article/details/6524767 FastMM是一个替换Embarcadero Delphi Win32应用程序的快速内存管理 ...
- MFC 定义和调用全局变量的一种方法
在CTestApp.h中声明一个int x;然后调用的方式如下: CTestApp *app = (CTestApp *)AfxGetApp(); //生成指向应用程序类的指针,Test处改为对应的项 ...
- Python int与string 的转换
string → int 1.10进制的string转化为int int('12') → type(int('12')) 进行验证 2.16进制的string转化为int int('12', 16) ...
- EF框架step by step(7)—Code First DataAnnotations(2)
上一篇EF框架step by step(7)—Code First DataAnnotations(1)描述了实体内部的采用数据特性描述与表的关系.这一篇将用DataAnnotations描述一下实体 ...
- python 代码片段18
#coding=utf-8 f=open('text.txt','w') f.write('foo\n') f.write('bar\n') f.close() f=open('test.txt',' ...