题目

假设你是一个专业的窃贼,准备沿着一条街打劫房屋。每个房子都存放着特定金额的钱。你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警

给定一个非负整数列表,表示每个房子中存放的钱, 算一算,如果今晚去打劫,你最多可以得到多少钱 在不触动报警装置的情况下

样例

给定 [3, 8, 4], 返回 8.

挑战

O(n) 时间复杂度 且 O(1) 存储。

解题

定义dp[i]表示打劫第i个房间为止所获得的最大收益,而dp[i]的值只与dp[i-2] 和dp[i-3]有关 并且 dp[i] = A[i] + max(dp[i-2],dp[i-3])

当求解所有的A[i]后,需要对最后两个dp[len-1] dp[len-2] 取最大值作为最后的答案。参考链接

public class Solution {
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public long houseRobber(int[] A) {
// write your code here
int len = A.length;
// dp[i] 表达打劫i房间为止所活动的收获 ,与dp[i-2] dp[i-3]有关
if(len ==0)
return 0;
long dp[] = new long[len];
dp[0] = A[0];
if(len == 1){
return dp[0];
}else if(len == 2){
dp[1] = A[1];
return Math.max(dp[0],dp[1]);
}else if(len == 3){
dp[1] = A[1];
dp[2] = A[0] + A[2];
return Math.max(dp[1],dp[2]);
}
dp[1] = A[1];
dp[2] = A[0] + A[2];
for(int i = 3;i< len; i++){
dp[i] = A[i] + Math.max(dp[i-2],dp[i-3]);
}
return Math.max(dp[len-2],dp[len-1]);
}
}

Java Code

总耗时: 3790 ms

不用数组,更改后的程序如下

public class Solution {
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public long houseRobber(int[] A) {
// write your code here
int len = A.length;
if(len == 0)
return 0; if(len == 1)
return A[0];
if(len == 2)
return Math.max(A[0],A[1]);
if(len == 3)
return Math.max(A[1],A[0] + A[2]);
long max0 = 0;
long max1 = 0;
long max2 = 0;
long max3 = 0;
max1 = A[0];
max2 = A[1];
for(int i = 2;i< len ;i++){
max3 = A[i] + Math.max(max0,max1);
max0 = max1;
max1 = max2;
max2 = max3;
}
return Math.max(max3,max1);
}
}

Java Code

讲解

  i-3 i-2 i-1 i  
  max0 max1 max2 max3  

对第i处的最大值max3 = A[i] + max(max1,max0)

当是i+1个的时候,更新max0、max1、max2

max0 = max1

max1 = max2

max2 = max3

主要对前三个的A[i]需要进行单独处理。

写成Python

class Solution:
# @param A: a list of non-negative integers.
# return: an integer
def houseRobber(self, A):
# write your code here
lenA = len(A)
if A == []:
return 0
if lenA == 1:
return A[0]
if lenA == 2:
return max(A[0],A[1])
dp = [0]*lenA
dp[0] = A[0]
dp[1] = A[1]
dp[2] = A[2] + A[0]
for i in range(3,lenA):
dp[i] = A[i] + max(dp[i-2],dp[i-3])
return max(dp[lenA-1],dp[lenA-2])

Python Code

总耗时: 814 ms

网上看到,也可以这样定义dp[i],表示当前所能获得的最大收获,这里的值最终就是最大值,由于数组dp的长度是len+1,第一个元素是0,dp[i]也可以理解为,不包含A[i]元素时所取得的最大值。

public class Solution {
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public long houseRobber(int[] A) {
// write your code here
int len = A.length;
if(len == 0)
return 0;
long dp[] = new long[len+1];
dp[1] = A[0];
if(len == 1)
return dp[1];
for(int i= 2 ;i<= len ;i++){
dp[i] = Math.max(dp[i-1],dp[i-2] + A[i-1]);
}
return dp[len];
}
}

不要数组

public class Solution {
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public long houseRobber(int[] A) {
// write your code here
int len = A.length;
if(len == 0)
return 0;
long res1 = 0;
long res2 = A[0];
if( len == 1)
return res2;
for(int i=1;i<len ;i++){
long res3 = Math.max(res2,res1+A[i]);
res1 = res2;
res2 = res3;
}
return res2;
}
}

lintcode:打劫房屋的更多相关文章

  1. lintcode:打劫房屋 III

    题目 打劫房屋 III 在上次打劫完一条街道之后和一圈房屋之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子组成的区域比较奇怪,聪明的窃贼考察地形之后,发现这次的地形是一颗二叉树.与前两次偷窃 ...

  2. lintcode:打劫房屋II

    题目 打劫房屋II 在上次打劫完一条街道之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子围成了一个圈,这就意味着第一间房子和最后一间房子是挨着的.每个房子都存放着特定金额的钱.你面临的唯一约 ...

  3. 打劫房屋 · House Robber

    [抄题]: 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警 ...

  4. Lintcode--011(打劫房屋2)

    在上次打劫完一条街道之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子围成了一个圈,这就意味着第一间房子和最后一间房子是挨着的.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子 ...

  5. lintcode算法周竞赛

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

  6. lintcode-392-打劫房屋

    392-打劫房屋 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自 ...

  7. 7九章算法强化班全解--------Hadoop跃爷Spark

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

  8. Java程序员秋招面经大合集(BAT美团网易小米华为中兴等)

    Cvte提前批 阿里内推 便利蜂内推 小米内推 金山wps内推 多益网络 拼多多学霸批 搜狗校招 涂鸦移动 中国电信it研发中心 中兴 华为 苏宁内推 美团内推 百度 腾讯 招商银行信用卡 招银网络科 ...

  9. 一个JAVA渣渣的校招成长记,附BAT美团网易等20家面经总结

    欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享美文,分享各种Java学习资源,面试题,以及企业级Java实战项目回复关键字免费领取): 今天分享一篇牛客网上的一个 ...

随机推荐

  1. html标记列表应用

    一.[ul]无序列表 1.无序列表====== 二.[ol]有序列表 1.有序列表用于段落有序的排列, <ol> <li>内容</li> </ol> 三 ...

  2. <梦断代码>读后感2

    <梦断代码>这本书读了一半,我的心情久久不能平静. 为什么好软件如此难做?这是我本人,我想也是很多人都在苦苦思索的一个问题,虽然没有人能有完全确定的答案,但通过书中的记述,和个人思考,还是 ...

  3. Java面试之SE基础基本数据类型

    1.九种基本数据类型的大小以及它们的封装类 在我们面试或者考试过程中经常会考到八种基本数据类型以及它们的封装类,那么有哪八种基本数据类型呢?它们的封装类又是什么呢? 首先,八种基本数据类型分别是:in ...

  4. 怎么用PHP发送HTTP请求(转载)

    本文转自:http://blog.snsgou.com/blog/161.html  file_get_contents版本: /** * 发送post请求 * @param string $url ...

  5. 【Binary Tree Level Order Traversal】cpp

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  6. ZeroMQ 在 centos 6.5_x86_64 下的安装

    ZeroMQ 在 centos 6.5_x86_64 下的安装 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.ZeroMQ介绍 ZeroMQ是一个开 ...

  7. 02.Apache FtpServer使用数据库管理用户

    1.创建数据库及表 使用\apache-ftpserver-1.0.6\res\ftp-db.sql建表,内容如下: CREATE TABLE FTP_USER ( userid VARCHAR(64 ...

  8. BZOJ3874 codevs3361 宅男计划

    AC通道1:http://www.lydsy.com/JudgeOnline/problem.php?id=3874 AC通道2:http://codevs.cn/problem/3361/ [题目分 ...

  9. Swift-6-函数

    // Playground - noun: a place where people can play import UIKit // 定义和调用函数 func sayHello(personName ...

  10. Win32 Plus Extra Height of Caption Bar

    you set the size of the non-client area by handling the WM_NCCALCSIZE message. But don't do this unl ...