题目:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

题目大意:

你是个专业盗贼,要偷遍一整条街,这条街每家每户里面都有钱,但是你不能偷相邻的两家,不然会触发警报,请问要怎么偷才能保证最大收益。

即是给予一个一维数组,存储的都是正数,求非连续最大和。


解:

这题属于一维简单DP问题,类似求数组连续最大和。

设len为数组长度,nums数组存储每家的存款,re数组存储对应长度数组的最大非连续和,即re[i]的值为,当数组长度为i时的非连续最大和。

如:

index       0  1   2       3       4

nums   5   2   4   7   1
re    5    5    9   12     12

len       1       2      3       4       5

状态转移:

假设要求re[n],因为不能取相邻的值, 这时必须考虑re[n-2], 所以re[n]=max(re[n-1], nums[n-1]+re[n-2])

初始态:

len=1, re[1]=nums[0]

len=2, re[2]=max(re[1], nums[1]+0)

len=3, re[3]=max(re[2], nums[2]+re[1])

...

由上可见求当前len=n只需由re数组的re[n-1], re[n-2]得出,所以只需用两个值保存前两个状态。


Java代码:

public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int a = 0, b = 0, tmp;
for(int i = 0; i<nums.length; i++){
tmp = b;
b = nums[i] + a > b ? nums[i] + a : b;
a = tmp;
}
return b;
}
}

Python代码:

class Solution:
# @param {integer[]} nums
# @return {integer}
def rob(self, nums):
a = b = 0
for i in xrange(len(nums)):
a, b = b, max(nums[i] + a, b)
return b

(DP)House Robber的更多相关文章

  1. leetcode动态规划笔记一---一维DP

    动态规划 刷题方法 告别动态规划,连刷 40 道题,我总结了这些套路,看不懂你打我 - 知乎 北美算法面试的题目分类,按类型和规律刷题 题目分类 一维dp House Robber : 求最大最小值 ...

  2. [LeetCode]House Robber II (二次dp)

    213. House Robber II     Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...

  3. Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)

    Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...

  4. 198. House Robber(Array; DP)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  5. LeetCode House Robber 家庭劫犯(dp)

    题意:有一个整数序列,从中挑出一些数字,使得总和是最大,前提是,相邻的两个数字中只能挑其一.比如1 2 3 就只能挑2或者1和3. 思路:很直观的题,dp思想.降低规模,从小规模开始考虑.如果只有两个 ...

  6. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  7. [LeetCode] House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. LeetCode House Robber III

    原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...

  9. DP专题训练之HDU 2955 Robberies

    打算专题训练下DP,做一道帖一道吧~~现在的代码风格完全变了~~大概是懒了.所以.将就着看吧~哈哈 Description The aspiring Roy the Robber has seen a ...

随机推荐

  1. HIbernate Oracle存储过程

    之前为了实现基于Hibernate+Oracle的存储过程调用,发现了一个又一个坑,然后一个一个的尝试解决. 需求:使用Hibernate调用Oracle的存储过程,需要支持的有动态表名.存储过程变量 ...

  2. 按每k个结点反转链表

    //按每k个结点反转链表 Node* turn_k(Node* head,int n,int k) { Node* p=head; ;t<k;t++) p=p->next; //为了获取最 ...

  3. 专访OPPO李紫贵:ColorOS用户过千万 软硬融合生态版图初现

    专访OPPO李紫贵:ColorOS用户过千万 软硬融合生态版图初现 专访OPPO李紫贵:ColorOS用户过千万 软硬融合生态版图初现

  4. STL中序列式容器的共性

    代码如下: /* * vector_1.cpp * * Created on: 2013年8月6日 * Author: Administrator */ #include <iostream&g ...

  5. Entify Framewrok - LINQ简单使用

    1.如何使用Join: http://www.devcurry.com/2011/01/join-example-in-linq-and-c.html

  6. poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

  7. jQuery----blur()方法

    当元素失去焦点时发生 blur 事件. blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时执行的代码. 提示:早前,blur 事件仅 ...

  8. ORACLE SEQUENCE 介绍

    在oracle中sequence就是所谓的序列号,每次取的时候它会自己主动添加,一般用在须要按序列号排序的地方.  1.Create Sequence  你首先要有CREATE SEQUENCE或者C ...

  9. WAS集群系列(6):集群搭建:步骤4:安装WAS升级软件

    逐步点击"下一步",注意一处流程,例如以下列举: "升级软件"安装的路径设置,建议与之前的WAS及IHS安装的绝对路径同样,例如以下所看到的: 逐步点击,完毕安 ...

  10. IPMITOOL常用操作指令

    一.开关机,重启 1. 查看开关机状态: ipmitool -H (BMC的管理IP地址) -I lanplus -U (BMC登录用户名) -P (BMC 登录用户名的密码) power statu ...