LintCode 392 House Robber
// 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的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- [LintCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LintCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- 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:二叉树下的不能相邻,求能 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
随机推荐
- 防止多次领取红包进行ID锁
//controller里面使用锁 ActivityRedPacket ap = customerService.getActivityRedPacket(params); if (synchr ...
- Sublime Text3 (转) 配置 以及快捷键配置
一.介绍 Sublime Text 是一款较新的编辑器,它轻量.简洁.高效,良好的扩展性以及跨平台等特性,使得越来越多的开发人员喜爱.它是一款收费的商业软件,但可以免费无限制无限期的试用,只会偶尔提醒 ...
- Java使用正则表达式取网页中的一段内容(以取Js方法为例)
关于正则表达式: 表1.常用的元字符 代码 说明 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串 ...
- asp.net 将word文档进行编辑并导出一个新的word
最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...
- maven仓库使用
maven镜像仓库 1.国内maven镜像仓库 阿里云镜像 <mirrors> <mirror> <id>aliyun</id> <name> ...
- 【随笔】Photoshop简单切图
一.首先准备一张需要切割的图: 二.打开Photoshop,在ps里打开这张图片: 三.在界面的左边选择切片工具: 四.根据需要,鼠标点住上方和左边的标尺拖动,给图片加上分割辅助线: 五.用切片工具点 ...
- windows环境下安装vue+webpack的开发环境
本人最近在学习vue,在学习的过程中遇到对的问题和解决方法 1.我们首先要安装node.js.node.js的官方地址为:https://nodejs.org/en/download/,下载完毕,按照 ...
- c#遍历目录及子目录下某类11型的所有的文件
DirectoryInfo directory = new DirectoryInfo("D:\\aa\\"); FileInfo[] files = directory.GetF ...
- DEV设计之自动流水号,DEV专家解答,自己折腾了半天也没有搞定,怪英文不好
() 老外专家给了回答,结果没有全到懂,又折腾了20分钟朋友提示才搞定 获取一个自动增加1的流水号值, 第一个参数是本事的数据库连接对象,第2个参数是也这个值为唯一标识返回来一个增量的值,第三个好像没 ...
- Goals100
Start:2016.4.10 100天目标:jy_ai学习.swift.设计模式 以10天为周期,开始周会,执行内容:自我检讨本周期,并展望下一个周期:目标一:寻找高效方法.1.思考, ...