题目:

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. 5分钟精通git教程

    git是一个版本控制工具,就要先弄清楚什么是版本 版本: 对外发布的版本如v1.0.0,v1.1.0 叫version 内部代码的版本叫commit,如:修改了一个错别字 顾名思义一个version就 ...

  2. 【公告】CSDN个人空间将于2014年4月20日全新改版上线

    尊敬的用户:   你们好!           CSDN个人空间将在2014年4月20日全新改版上线!        CSDN个人空间是2008年8月推出的服务,致力于给广大用户提供在线技术分享和资料 ...

  3. Android开发之DatePickerDialog与TimePickerDialog的功能和使用方法具体解释

    DatePickerDialog与TimePickerDialog的功能比較简单,使用方法也非常easy.仅仅要以下两步就可以. Ø  通过newkeyword创建DatePickerDialog.T ...

  4. Android Studio使用技巧系列教程(二)

    尊重劳动成果,转载请注明出处:http://blog.csdn.net/growth58/article/details/46764575 关注新浪微博:@于卫国 邮箱:yuweiguocn@gmai ...

  5. (转)CommandArgument用法

    1.绑定数据库中一个主键前台代码:<ItemTemplate>                        <asp:ImageButton ID="ibtnUpdate ...

  6. Win 7 通过事件管理器查看计算机开机关机时间

    控制面板-管理工具-事件查看器 视图中开机来源:Kernel-General 事件ID:13 关机来源:Kernel-General 事件ID:12

  7. c++ 编译期计算 (一)

    编译期就是编译器进行编译,产生.obj文件的所处的那一段时间(如果是广义的编译期,那么一般还包括了链接期,因为现在很多编译器都会自动调用链接器进行链接)执行期就是你执行某个已经链接好的程序的那段时间. ...

  8. hdu5353 Average(模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...

  9. [转载]Git常用命令

    转载自: Git常用命令 Git配置 git config --global user.name "robbin" git config --global user.email & ...

  10. worklight 中添加时间控件

    在我们使用worklight开发的过程中,由于文档的不开源和插件的缺少,总是自己琢磨很多东东,更有胜者 需要调用源代码实现某些不易实现的功能.在这里把实现的功能代码贴出来,如有不足之处还望指正! 实现 ...