题目:

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. java.sql.SQLException:指定了无效的 Oracle URL

    java.sql.SQLException:指定了无效的 Oracle URL 昨天晚上用MyEclipse连接Oracle,出现了" java.sql.SQLException: 指定了无 ...

  2. testng xml 示例

    TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.php 更多testng配置及说明,请移步http://testdoc.org/docmaster?pi ...

  3. python高级编程:有用的设计模式3

    # -*- coding: utf-8 -*-__author__ = 'Administrator'#python高级编程:有用的设计模式#访问者:有助于将算法从数据结构中分离出来"&qu ...

  4. python中os模块常用方法

    #!/usr/bin/python## os module test import os print 'os.name: ', os.nameprint 'os.getcwd(): ', os.get ...

  5. AJAX上传文件

    function up_files() { var fileSelect = document.getElementById('file-select'); var files = fileSelec ...

  6. HDU 2689 sort it - from lanshui_Yang

    Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent ...

  7. 在线C语言编译器/解释器

    在线C语言编译器/解释器 本文介绍两个C语言在线解释器/编译器,这些工具可以提高代码片段检测方便的工作效率,并可以保证这些代码的正确性,而且还可以和别人一起编辑/分享之间的代码,这样可以共同分析代码并 ...

  8. [Redux] Generating Containers with connect() from React Redux (VisibleTodoList)

    Learn how to use the that comes with React Redux instead of the hand-rolled implementation from the ...

  9. js jsp 时间 日期 控件 插件 简单 实用

    js时间控件一般都是找网上的用,这东西平常很少涉及到,一用到找起来却烦死人,不是没用就是太复杂,今天向大家推荐一个简单实用的控件,该控件在不断更新,而且有专门的网站对它进行维护,所以值得一看. 先说它 ...

  10. (转)Div+CSS布局入门

    在网页制作中,有许多的术语,例如:CSS.HTML.DHTML.XHTML等等.在下面的文章中我们将会用到一些有关于HTML的基本知识,而在你学习这篇入门教程之前,请确定你已经具有了一定的HTML基础 ...