// Ref: https://segmentfault.com/a/1190000003811581
// Ref: http://www.cnblogs.com/grandyang/p/4383632.html

/*
如果选择了抢劫上一个屋子,那么就不能抢劫当前的屋子,所以最大收益就是抢劫上一个屋子的收益
如果选择抢劫当前屋子,就不能抢劫上一个屋子,所以最大收益是到上一个屋子的上一个屋子为止的最大收益,加上当前屋子里有的钱
*/

 public static long houseRobber(int[] A) {
int len = A.length;
if (len <= 1) {
return len == 0 ? 0: A[0];
} long previous = A[0]; // a is the previous max
long current = Math.max(A[0], A[1]); // b is the current max
for(int i = 2; i < len; i++){
long tmp = current;
// previous + A[i] => pre-pre + current
// b/c it cannot rob adjacent houses => need to compare
current = Math.max(previous + A[i], current);
previous = tmp;
}
return current;
}

LintCode 392 House Robber的更多相关文章

  1. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  2. [LintCode] House Robber II 打家劫舍之二

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

  3. [LintCode] House Robber 打家劫舍

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

  4. [LintCode] House Robber III 打家劫舍之三

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

  5. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

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

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

随机推荐

  1. mac--又发现了一款mac快捷键神器

    之前用applescript,还有keyboard maestro,前段时间用机械键盘的时候,用开源的karabiner重新设置机械键盘的键位. 今天因为网上抄的键位F4想换成Launchpad,所以 ...

  2. 一个MVC架构的线程安全的银行转账案例(事务控制)

    mvc结构: 准备阶段:jar包 ,dbcpconfig.propertie(数据源配置文件 ) ,DBCPUtil. jar包: dbcp配置文件: driverClassName=com.mysq ...

  3. redirect和forward

    1.重定向 <mvc:view-controller path="/" view-name="redirect:/admin/index"/>即如果 ...

  4. HTML和CSS设置动态导航以及CSS中伪元素的简单说明

    HTML页面代码: <!DOCTYPE html> <html> <head> <title>Test</title> <meta c ...

  5. 数字与字母 随机数小demo

    public String generateRandomId(){ String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHI ...

  6. Unity3d Android Http 开发中的坑(吐槽

    在一般的U3D网络开发中,直接使用WWW类便足够正常使用,但我在发现使用WWW下载大文件时,会导致整个程序卡顿的情况(不清楚是否我个人电脑问题),所以干脆使用HttpWebRequest/HttpWe ...

  7. sql sever获取数据库还原时间语句

    --只获取数据库名称和最后的还原时间 SELECT sdb.Name AS DatabaseName , ), ), '-') AS LastBackUpTime FROM sys.sysdataba ...

  8. 扩大a标签的响应区域

    <a href="" style="display:inline-block; width: 100%;">xx</a>

  9. Java变量自增和自减运算符的用法

    1.后加加(num++): 先输出运算结果再加加: public static void main(String[] args){ int num=10; int  p1=num++; System. ...

  10. java抽象语法

    1.基本概念: 0.0.抽象类的定义:抽象类是为子类提供一个规范,其目地是由子类去继承实现(类似国家提出的法律,由我们去执行). 0.1.定义抽象类用abstract来定义. 语法: public a ...