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. jquery IE6 select.val() bug报错解决办法

    原文地址:http://hi.baidu.com/kinghmx/item/395dbac3261292dcef183b52 最近在写一个页面,在出了ie6外的所有浏览器中都正常(ie7,8,9,  ...

  2. yum笔记

    rpm --> yum HTML: HyperText Mark LanguageXML: eXtended Mark Language XML, JSON: 半结构化的数据 yum仓库中的元数 ...

  3. Linux TCP/IP 协议栈之 Socket 的实现分析(一)

    内核版本:2.6.37参考[作者:kendo的文章(基于内涵版本2.6.12)] 第一部份 Socket套接字的创建 socket 并不是 TCP/IP协议的一部份. 从广义上来讲,socket 是U ...

  4. 【多线程】--生产者消费者模式--Lock版本

    在JDK1.5发布后,提供了Synchronized的更优解决方案:Lock 和 Condition 我们使用这些新知识,来改进例子:[多线程]--生产者消费者模式--Synchronized版本 改 ...

  5. Html中value和name属性的作用

    1.按钮中用的value 指的是按钮上要显示的文本  比如“确定”“删除”等 2.复选框用的value 指的是这个复选框的值 3.单选框用的value 和复选框一样 4.下拉菜单用的value 是列表 ...

  6. Lua内存泄漏应对方法[转]

    转自http://blog.csdn.net/xocoder/article/details/42685685 由于目前正在负责的项目是一个二次开发项目,而且留给我们的代码质量实在让人无力吐槽,所以遇 ...

  7. 实现Android操作系统11种传感器介绍

    在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER 1 //加速度 #define ...

  8. Android进程间通信(IPC)机制Binder简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6618363 在Android系统中,每一个应用 ...

  9. iOS-设计模式之Block

    Block是代码块, Block定义 返回值 (^ 块名)(参数1,参数2…); 在定义Block的时候可以使用typedef 重命名一下. typedef void(^blockName)(NSSt ...

  10. Unity轻量级依赖注入容器

    一.前言 Unity是一个轻量级的可扩展的依赖注入容器,支持构造函数,属性和方法调用注入.在Nuget里安装unity