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 解答的更多相关文章

  1. House Robber II 解答

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

  2. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  3. [LeetCode] House Robber III 打家劫舍之三

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

  4. [LeetCode] House Robber II 打家劫舍之二

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

  5. [LeetCode] House Robber 打家劫舍

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

  6. 精选30道Java笔试题解答

    转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...

  7. 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...

  8. 全部leetcode题目解答(不含带锁)

    (记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...

  9. 【leetcode】House Robber

    题目简述 You are a professional robber planning to rob houses along a street. Each house has a certain a ...

随机推荐

  1. HDU 1074 Doing Homework (状态压缩 DP)

    题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...

  2. 线段树(build,insert,dfs操作)

    模板原型: 解决零散数点在已知线段上的出现次数.思想是将线段用长线覆盖,将长线转化成线段树.用权值记录各个数点出现的次数,最后进行查询.代码解释见注释. #include <bits/stdc+ ...

  3. win10无法使用内置管理员账户打开

    对于这种问题其实对于专业版用户倒不是难事,很容易解决,具体方法如下: 首先WIN+R输入:gpedit.msc,打开组策略,找到“计算机配置”里面的“Windows设置”,打开“安全设置” -> ...

  4. GCD 延时操作

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_ ...

  5. 泛泰A880S升级官方4.4.2 binx教程

    之前为了尝鲜,直接官升4.4.2,红砖了.越南兄弟说官方没更新升级工具,所以导致升级到78%就停止了,他给的办法,我试也没成功.官方旧版本号又不能升级S0221118以上的版本号,新版的离线升级工具没 ...

  6. ECMAScript 6新特性介绍

    箭头函数 箭头函数使用=>语法来简化函数.在语句结构上和C#.Java 8 和 CoffeeScript相似,支持表达式和函数体. . =>`操作符左边为输入的參数.而右边则是进行的操作以 ...

  7. hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)

    Problem Description 小明自从告别了ACM/ICPC之后,就開始潜心研究数学问题了,一则能够为接下来的考研做准备,再者能够借此机会帮助一些同学,尤其是美丽的师妹.这不,班里唯一的女生 ...

  8. C++菱形继承的构造函数

    网上搜了很多,大多是关于菱形虚继承的构造函数应该怎么写,或者就是最简单的,四个类都不带参数的构造函数. 本文旨在记录一下困扰了博主1h的问题,非常浅显,有帮助固然好,如果侮辱谁的智商还见谅,当然无限欢 ...

  9. 《JavaScript 闯关记》之 BOM

    ECMAScript 是 JavaScript 的核心,但如果要在 Web 中使用 JavaScript,那么 BOM(浏览器对象模型)则无疑才是真正的核心.BOM 提供了很多对象,用于访问浏览器的功 ...

  10. 1203.4——循环语句 之 for

    for循环的一般形式为:for(表达式1; 表达式2; 表达式3){    语句块} 它的执行过程如下:1) 先求解表达式1. 2) 求解表达式2,若其值为真(非0),则执行循环体,否则结束循环. 3 ...