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 ...
随机推荐
- iOS代码实现九宫格
#define ScreenW [UIScreen mainScreen].bounds.size.width #define ScreenH [UIScreen mainScreen].bounds ...
- JsonString,字典,模型之间相互转换
NSData转字符串 [NSString alloc] initWithData: encoding:] 模型转字典 attInfo.keyValues 字典转模型 ZTEOutputInfo *ou ...
- 空MVC项目找不到System.Web.Optimization的处理办法
install-package Microsoft.AspNet.Web.Optimization Create the bundle in Global.asax Application_Start ...
- linux环境下配置solr5.3详细步骤
本人上周五刚刚配置了一遍centos下配置solr5.3版本,综合借鉴并改进了一些教程,贴出如下 单位使用内网,本教程暂无截图,抱歉 另,本人是使用.net编程调用solr的使用的是solrnet,在 ...
- DataSet用法详细 转
DataSet用法详细 转 DataSet用法详细 一.特点介绍 1.处理脱机数据,在多层应用程序中很有用. 2.可以在任何时候查看DataSet中任意行的内容,允许修改查询结果的方法. 3.处理分级 ...
- 深入浅出: Java回调机制(异步)
一.什么是回调 回调,回调.要先有调用,才有调用者和被调用者之间的回调.所以在百度百科中是这样的: 软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分为三类:同步调用.回调和异步调用. 回调 ...
- linux 查看php-fpm 进程数
netstat -napo |grep "php-fpm" | wc -l
- AJAX应用优势
国内翻译(仅音译)常为 “阿贾克斯” 和阿贾克斯足球队同音. 使用ajax 构建应用程序 这个术语源自描述从基于 Web 的应用到基于数据的应用的转换.在基于数据的应用中,用户需求的数据如联系人列表, ...
- flex柱状图
柱状图的展现是通过flex自带的控件实现 控件解析:<mx:ColumnChart id = "columns" dataProvider = "{dataSour ...
- Eclipse在线集成maven M2eclipse插件
首先说下版本: Eclipse:3.6 Maven:3.3.1,若不知道如何在本地安装Maven,请参见我的另一篇文章:Window下安装Maven 废话少说,直接讲步骤就好: 1.打开eclipse ...