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. Redis分布式之前篇

    第一篇:初识Redis 一.Redis是什么? Redis 是一个开源(BSD许可)的,使用ANSI C语言编写的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据 ...

  2. deep_learning_Function_tensorflow_transpose()

    tf.transpose()的用法 一.tensorflow官方文档内容 transpose(     a,     perm=None,     name='transpose' ) Defined ...

  3. 六、取消eslint 校验代码

    一.取消eslint 校验代码 删除 "eslintConfig": { "root": true, "env": { "node ...

  4. Single List Reversion

    LeetCode 1. 基于头插法的迭代: public ListNode reverseList(ListNode head) { if(head == null) return null; Lis ...

  5. Spring EntityResolver ".dtd" 和 ".xsd"检验

    XmlBeanDefinitionReader 加载xml EntityResolver entityResolver; ErrorHandler errorHandler = new SimpleS ...

  6. Spring相关概念

    DIP: Dependency Inversion Principle.翻译过来是依赖反转原则,也叫依赖倒置原则. 依赖倒置原则是设计模式几个重要原则之一.具体定义就是,底层模块依赖高层模块定义的接口 ...

  7. Linux服务器安装配置JDK

    一.准备工作: 1.登录服务器,切换到root用户(su - root,然后输入密码,按enter),进入根目录:cd / 2.进入要安装jdk的目录,自己可以创建一个java目录,执行命令如下: c ...

  8. 【CF1181C】Flag

    题目大意:给定一个 N*M 的矩阵,定义一个矩形区域为一个"国旗",满足:矩形区域可以按行划分成三个高度相同的部分,其中每一个部分中的颜色完全相同,第一部分的颜色与第二部分颜色不同 ...

  9. sudo 不用输入密码

    3. 设置当前登陆用户免密 使用visudo打开sudoers并编辑 sudo visudo 在刚才编辑的内容中加上NOPASSWD: linuxidc ALL=(ALL:ALL) NOPASSWD: ...

  10. 基于idea的maven(一)Maven的安装

    1.Maven前置依赖 检查电脑是是否安装java 2.下载maven 网址 www.apache.org 解压 maven 压缩包, 并创建相应的maven本地仓库的路径. 打开 conf文件夹中 ...