mycode

思路:

a:1 2 3 4 5 6 7 8 9

f(9) =max( f(7) + a9 ,f(8)) 前一步、前两步

至于前三步 f(9) = f(6)+ a9,但其实f(7)在求值的时候按照上面的公式一定是比f(7)大于等于的,所以f(6)+a9总是小于等于上面的递推式的

至于前四步更不用考虑了,因为前两步已经考虑了前四步

time limited

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
elif len(nums) == 1:
return nums[0]
elif len(nums) == 2:
return max(nums)
return max(self.rob(nums[:-2])+nums[-1],self.rob(nums[:-1]))

思路上的难点:

a :1 2 3 4 5 6 7 8

比如a4的时候,有以下选择:f(2) + a4  ,f(3) ,至于f(1)+a4不需要再去考虑,因为f(2)一定是大于等于f(1)的

参考

1、上面是递归求解,然而复杂度太高无法AC。所以应该记录已经计算过的结果,于是这变成一个动态规划问题

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 0
elif len(nums) < 2:
return max(nums[0], nums[-1])
money = [0]*len(nums)
money[0], money[1] = nums[0], max(nums[0], nums[1])
for i in xrange(2, len(nums)):
money[i] = max(nums[i] + money[i-2], money[i-1])
return money[len(nums)-1]

2、

上面的代码使用的空间是冗余的,因为每次循环只会用到前两个数据。所以代码可以降低空间复杂度到O(1)。

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
now = last = 0
for i in nums:
last, now = now, max(i+last, now)
return now

leetcode-easy-dynamic-198 House Robber-NO的更多相关文章

  1. 【leetcode❤python】198. House Robber

    class Solution(object):    def rob(self, nums):        """        :type nums: List[in ...

  2. 【easy】198. House Robber 123总结……

    题目一: 一个极其简单的动态规划. class Solution { public: int rob(vector<int>& nums) { ; // 表示没有选择当前house ...

  3. 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:二叉树下的不能相邻,求能 ...

  4. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  5. 198. House Robber(动态规划)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  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. (easy)LeetCode 198.House Robber

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

  8. leetcode 198. House Robber (Easy)

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

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

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

  10. 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...

随机推荐

  1. 浅尝https

    HTTPS http超文本传输协议,所以的东西都是明文传输,容易被拦截,被攻击,我们希望能对通话内容进行加密,那么因此而生,出现了https https:在http的基础上新增加了SSL层 先放图 / ...

  2. Reference to ‘xxxxx’ is ambiguous 错误

    1.原因,在当前类重复引入了 类库,比如 pch里面导入了#import "XXX" 此类的.h 又引入 #import <xxx/xxx> 导致 解决方法:删除此类的 ...

  3. 关于hdfs的一些认知

    先从网上copy一些优势点 1.高容错性数据自动保存多个副本.它通过增加副本的形式,提高容错性.某一个副本丢失以后,它可以自动恢复,这是由 HDFS 内部机制实现的,我们不必关心. 2.适合批处理它是 ...

  4. 新增分区格式化时提示设备文件不存在:--- No such file or directory的处理方法

    [原文链接]:http://blog.itpub.net/28874898/viewspace-774249/ 在系统中的空余空间添加新的分区:   fdisk   /dev/sda (第一块硬盘上) ...

  5. poj 2081 Recaman's Sequence (dp)

    Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 22566   Accepted: 96 ...

  6. 终于要开始做大名鼎鼎的BombLab了!

    首先是一些准备工作 lab下载地址:http://csapp.cs.cmu.edu/3e/labs.html 第二个的Bomblab的 self-study handout就是 在做这个lab前,首先 ...

  7. 爬虫之如何找js入口(一)

    目标网页:https://m.gojoy.cn/pages/login/ 将我删除i ndex?from=%2Fpages%2Fuser%2Findex 需要工具:chrome和油猴 油猴代码: // ...

  8. Python enumerate 使用技巧

    enumerate() 是Python内建的函数,能让打印的结果更清晰,不管是列表,元组,字典,enumerate()都可以帮你完成,在某些需求下还是非常好用的. >>> a = [ ...

  9. .htaccess防盗链方法(文件、图片)

    http标准协议中有专门的字段记录referer,一来可以追溯上一个入站地址是什么,二来对于资源文件,可以跟踪到包含显示他的网页地址是什么. 因此所有防盗链方法都是基于这个Referer字段两种方法: ...

  10. springboot-activiti TaskLISTener无法注入service

    转自CSDN :https://blog.csdn.net/Laiguanfu/article/details/89366193 第一步创建springUtil类 @Componentpublic c ...