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。例如 nums = [2, 3, 1, 5, 7, 8]

dp[0] = 2, dp[1] = max(nums[0], nums[1]) 这两个点需要单独操作。 dp[i]的值取决于[i]这一家是不是要偷。如果不偷的话,dp[i] = dp[i-1]。如果偷的话,[i-1]这一家就不能偷,所以 dp[i] = dp[i-2] + nums[i]。所以dp[i] = max(dp[i-1], dp[i-2]+nums[i])。

     def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
n = len(nums)
if n == 1:
return nums[0]
if n == 2:
return max(nums[0], nums[1]) dp = [0]*n
dp[0] = nums[0]
dp[1] = max(nums[1], nums[0]) for i in range(2, n):
dp[i] = max(dp[i-2]+nums[i], dp[i-1]) return dp[-1]

本质上并不需要保存dp list中的所有值,只需要保存两个,分别对应even and odd position。 这样的话可以写成:

     def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a, b = 0, 0
n = len(nums) for i in range(n):
if i % 2 == 0:
a += nums[i]
a = max(b, a)
else:
b += nums[i]
b = max(a, b) return max(a, b)

Leetcode 198 House Robber的更多相关文章

  1. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  2. [LeetCode] 198. House Robber 打家劫舍

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

  3. Java for LeetCode 198 House Robber

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

  4. (easy)LeetCode 198.House Robber

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

  5. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

  6. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

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

  7. leetcode 198. House Robber (Easy)

    https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...

  8. Leetcode 198 House Robber 动态规划

    题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...

  9. leetcode 198 House Robber I

    function rob(nums) { if(!nums || nums.length === 0) { return 0; } else if(nums.length < 2){ retur ...

随机推荐

  1. vuejs全局api

    全局api set 增加数组 vm.$set 实例化方法 全局api delete 删除数组 vm.$delete 实例化方法 全局 api 组件component 实例化方法 components ...

  2. zookeeper多节点配置

    单机多节点模式 zookeeper解压, 放到 /opt/zookeeper/ 下, 同目录再放一个 server1目录, 下面建data和log两个目录用于存放数据和日志 zoo.cfg [milt ...

  3. AngularJS中的过滤器

    欢迎大家指导与讨论 : ) 一.前言 AngularJS的过滤器能够将数据在被指令处理到显示在视图之前进行处理和转换.而且,过滤器不会修改作用域中的数据本身,即过滤器会保证数据的完整性.这样子能够允许 ...

  4. swift 定时器的使用

    在swift中,要使用定时器就需要用到对象NSTimer.通过NSTimer的实例化后,就可以调用fire方法来启用了. NSTimer有2个构造函数 init(timeInterval ti: NS ...

  5. 投入Html5的怀抱,最近在研究的Egret

    html5没有办法不关注,实在太火热了,几年前还不行,如今确是环境较好,typescript语言很好学习,可能基于之前的基础,不到一个星期就基本上差不多了,虽然还有一些小问题,但那都是经验积累下来可以 ...

  6. vs 2005 thread 无法调试

    两种办法:1.打开项目属性,在“Debug”一项里,把“Enable the Visual Studio hosting process”前的钩去掉.这个方法不是好办法.2.打开计算机管理,在服务里将 ...

  7. linux传输大文件

    http://dreamway.blog.51cto.com/1281816/1151886 linux传输大文件

  8. Gruntjs: grunt-contrib-jst

    预编译Underscore模板到JST文件(Underscore:JS工具库) generate JavaScript template functions Gruntfile的配置实例: modul ...

  9. Java程序设计的DOS命令基础

    Java程序设计的DOS命令基础 用户使用操作系统和软件有两种方式:命令行界面(Command Line Interface,CLI)和图形界面(Graphical User Interface,GU ...

  10. CUDA2.1-原理之索引与warp

    本小节来自<大规模并行处理器编程实战>第四节,该书是很好的从内部原理结构上来讲述了CUDA的,对于理解CUDA很有帮助,借以博客的形式去繁取间,肯定会加入自己个人理解,所以有错误之处还望指 ...