// 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. 做webapp静态页面的一些积累

    ​1)根据pad,手机不同的版本的屏幕大小设置字体的大小(可以使用此方式根据屏幕的不同进行设置,由于我左边的图片是设置的float='left',使用的是img标签的百分比来显示图片) (使用此方式, ...

  2. python autopep8

    安装 使用pip install autopep8或easy_install 都可以. 使用 autopep8 -i -a 要检查的py文件路径 更多参数使用可以参考:https://github.c ...

  3. 利用Maple推导向量方程的微分公式

    在某些几何软件的开发中,会要求写出一个向量方程的微分公式.对我而言,手工推导繁琐.易出错.且需要反复校验. 早就听说Mathematica, Maple这样的软件可以自动进行符号公式的推导,一直没有时 ...

  4. Ext.encode 抛出异常“Uncaught RangeError: Maximum call stack size exceeded”

    在用使用Ext.encode(ExtObject)过程中抛出了如下错误: Uncaught RangeError: Maximum call stack size exceeded 实际上,不能用 E ...

  5. mysql三种注释方法

    SELECT * from test;#test表select * from user;-- 用户表select * from tb_test_paper;/*试卷表*/

  6. vpython初探

    vpython 是python默认的3D模块,和python有一样的风格.与PyOpenGL相比,容易上手. vpython下载:vpython的官网(www.vpython.org).顺便说一句,官 ...

  7. json改造优化无刷新分页

    index.html dd

  8. mysql galera cluster 集群的分裂与仲裁机制

    集群的分裂 当集群由于网络原因分裂为几个单独的组时(一组可能是单节点,也可能是几个互联的节点),数据出现不一致,此时可能产生脑裂及数据不一致.这种情况 下,只有一组节点能够继续提供服务,这组节点的状态 ...

  9. NSURLSession的基本使用

    一.简单说明 在iOS9.0之后,以前使用的NSURLConnection过期,苹果推荐使用NSURLSession来替换NSURLConnection完成网路请求相关操作. NSURLSession ...

  10. jquery easyui的treegrid的控制

    其中列的formatter很有作用,可以得到你想要的任何内容: 例: <table class="easyui-treegrid" id="tg" dat ...