House Robber 解答
Question
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.
Solution 1 -- DP
The key is to find the relation dp[i] = Math.max(dp[i-1], dp[i-2]+num[i-1]). Time complexity O(n), space cost O(n).
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length < 1)
return 0;
int length = nums.length;
int[] dp = new int[length + 1];
dp[0] = 0;
dp[1] = nums[0];
for (int i = 2; i <= length; i++) {
dp[i] = Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]);
}
return dp[length];
}
}
Solution 2
Another method is to use two counters, one for even number and the other for odd number.
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length < 1)
return 0;
int odd = 0, even = 0, length = nums.length;
for (int i = 0; i < length; i++) {
if (i % 2 == 0) {
even += nums[i];
even = even > odd ? even : odd;
} else {
odd += nums[i];
odd = odd > even ? odd : even;
}
}
return even > odd ? even : odd;
}
}
House Robber 解答的更多相关文章
- House Robber II 解答
Question After robbing those houses on that street, the thief has found himself a new place for his ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- [LeetCode] 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 II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
- 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- 【leetcode】House Robber
题目简述 You are a professional robber planning to rob houses along a street. Each house has a certain a ...
随机推荐
- Java宝典(四)------Java中也存在内存泄露。
--Java中会存在内存泄露吗? --如果你想当然的以为Java里有了垃圾回收机制就不会存在内存泄露,那你就错了. Java里也会存在内存泄露! 我们慢慢来分析. 所谓内存泄露就是指一个不再被程序使用 ...
- 用特殊字体来实现矢量ICON
用特殊字体来实现矢量ICON tips:其实每个icon都是一个unicode字符,所以,可以通过改变font-size实现icon的矢量放大:可以通过改变color实现多色系.
- <转载>Wait and Waitpid
转载http://www.cnblogs.com/lihaosky/articles/1673341.html 一.Wait #include <sys/types.h> /* 提供类型p ...
- [置顶] java ant 配置及构建项目
Ant是一种基于Java的构建工具.Ant文件是配置构建目标过程的XML文件,也称为Ant脚本. (因为对这个不是很了解,所以用词方面可能于个人的理解有偏差 ...
- MySQL中多表删除方法(转载)
如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...
- Non-negative Partial Sums(单调队列)
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- AC Milan VS Juventus(模拟)
AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Oth ...
- PHP你可能也会掉入的坑
今天被人问: $var = 'test'; if (isset($var['somekey'])) { echo 'reach here!!!'; } 会不会输出'reach here!!!'? -- ...
- 3. QT窗体间值的传递(续)
一.前言 上篇博客中通过重载子窗体的构造函数将主窗体的值传入到子窗体,但是在子窗体运行过程中如何才能将值动态的传入到子窗体?可以有两种办法,1.信号和槽的方式传值:2.主窗体中将传出值设置为publi ...
- JavaScript-------寄生组合式继承
组合继承在前面有说过,也是JavaScript中最常用的一个继承模式:不过,它也有自己的不足.组合继承最大的问题就是无论什么情况,都会调用两次构造函数: 那我们来回顾下组合式继承基本模式: funct ...