// 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. 放假回来啦!!小技能:一个div不给width,怎么让它居中捏?`(*∩_∩*)′

    答案是:这个div没有浮动的话,就用text-align: center; 有的话...我也不知道了

  2. WIN7环境下CUDA7.5的安装、配置和测试(Visual Studio 2010)

    以下基于"WIN7(64位)+Visual Studio 2010+CUDA7.5". 系统:WIN7,64位 开发平台:Visual Studio 2010 显卡:NVIDIA ...

  3. notepad++的CoolFormat代码格式化插件使用

    因为notepad++的NppAStyle插件只支持格式化C.C++.C#.Java这四种编程语言的代码,所以本人推荐使用这个CoolFormat的插件,相比于NPPAStyle,CoolFormat ...

  4. shell 中scp密码输入 --expect

    这里必须先安装: yum install expect -y expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录. 下面给出scp和ssh的使用示例: 1. ...

  5. 修改页面JS 360浏览器

    javascript:document.body.contentEditable='true'; document.designMode='on'; void 0

  6. css3中单位px,em,rem,vh,vw,vmin,vmax的区别及浏览器支持情况

    原文地址: http://blog.csdn.net/jyy_12/article/details/42557241 px:绝对单位,页面按精确像素展示 em:相对单位,基准点为父节点字体的大小,如果 ...

  7. linux下的redis安装以及php添加redis扩展

    一.redis的安装 win版本详见: 下面是linux版本的安装步骤: step1.下载 http://redis.io/download下载完后直接make然后make install,注意sud ...

  8. OpenMP并行构造的schedule子句详解 (转载)

    原文:http://blog.csdn.net/gengshenghong/article/details/7000979 schedule的语法为: schedule(kind, [chunk_si ...

  9. Co-saliency-Huazhu Fu

    这里主要是fu老师的显著性检测分割的一些资料. 对应的主页为:http://hzfu.github.io/ 对应的一些codes:https://github.com/HzFu

  10. 微信公众账号开发之N个坑(二)

    上篇说到微信公众账号的几个坑,前面五个,已经说到菜单,宝宝继续往下赘述了.可惜,还不知道宝宝的宝宝到底是不是心疼宝宝呢,完了,我凌乱了... 回到正题,我们就不吐槽其他的了,上一篇说到微信的菜单了,那 ...