[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 ...
随机推荐
- 1:A+B Problem
总时间限制: 1000ms 内存限制: 65536kB 描述 Calculate a + b 输入 Two integer a,,b (0 ≤ a,b ≤ 10) 输出 Output a + b ...
- GitHub 使用教程图文详解(转)
大纲: 一.前言 二.GitHub简介 三.注册GitHub账号 四.配置GitHub 五.使用GitHub 六.参与GitHub中其它开源项目 七.总结 注,GitHub官网:https://git ...
- python特殊函数 __call__()
__call__ 在Python中,函数其实是一个对象: >>> f = abs >>> f.__name__ 'abs' >>> f(-123) ...
- linux 查看机器的cpu,操作系统等命令
看cpu信息,型号,几核 [root@f3 ~]# cat /proc/cpuinfo | grep name | cut -f2 -d:| uniq -c 16 Intel(R) Xeon(R) C ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- Codeforces 629C Famil Door and Brackets(DP)
题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...
- static方法中为什么使用的都是静态变量
static方法中需要使用静态变量.假设其他类要使用该方法,该方法里面所使用的变量是非静态的,如果该方法所在的类没有实例化,会导致该方法里面的变量不能实例化,自然该static 方法不能使用
- 51nod p1175 区间中第K大的数
1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...
- Pick-up sticks[HDU1147]
Pick-up sticksTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- BZOJ4380 : [POI2015]Myjnie
将$c$离散化,设: $f[i][j][k]$为区间$[i,j]$最小值为$k$的最大收益. $g[i][j][k]$为$\max(g[i][j][k..m])$. $h[i][j]$为对于当前DP区 ...