您的位置:首页 > 其它

lintcode 中等题:House Robber 打劫房屋

2015-11-18 17:37 351 查看
题目

打劫房屋

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

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

样例

给定
[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] 取最大值作为最后的答案。参考链接

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: