题目简述

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.

解题思路

很明显是个动态规划问题,这里的转移方程也很明显,res[i] = max(res[i - 1] , res[i - 2] + num[i-2])。res表示当前我们到这一家所能够获得的最大的利润。这个利润简单地说就取决于强不强这家,并且注意限制是不能抢连续的两家。

class Solution:
# @param num, a list of integer
# @return an integer
def rob(self, num):
res = [0] * (len(num) + 2)
for i in range(2,len(num)+2):
res[i] = max(res[i - 1] , res[i - 2] + num[i-2])
return res[-1]

【leetcode】House Robber的更多相关文章

  1. 【leetcode】House Robber & House Robber II(middle)

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

  2. 【LeetCode】House Robber III(337)

    1. Description The thief has found himself a new place for his thievery again. There is only one ent ...

  3. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. javascript中this

    js函数中的this指向问题:(this对象是在运行时基于函数的执行环境绑定的) 与普通变量获取不同,内部函数在搜索this和arguments这两个变量时,只搜索到其活动对象为止,所以this无法像 ...

  2. p/invoke 碎片-- 对字符串的处理

    字符串在内存中的的几种风格 字符串作为参数和返回值 参考 字符串在内存中的几种风格 所谓的风格,也就是字符串在内存中的存在形式.如何存放的,占据内存的大小,还有存放顺序等.在不同的编程语言和不同的平台 ...

  3. iTunes Connect 显示可供销售,但是AppStore 就是不显示新版本(异于往常版本更新)

    这次版本更新,从上传到审核通过不足8小时.由于是手动发布,第二天早上上班发布了新版本.但是不同于往常,这次等了很久也不见AppStore 更新新版本.检查一下iTunes Connect ,显示可供销 ...

  4. Python

    语法 #!/usr/bin/python 注释:# 编码:# -*- coding: UTF-8 -*- 缩进 运算符 算数运算符 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得 ...

  5. MyBatis日志配置

    关于MyBatis的日志,其实MyBatis已经弄得很好了,你甚至都不用配置,只要导入了jar包,MyBatis就会自动寻找. 具体步骤 1.导入jar包,就是把下载MyBatis时,lib里的包复制 ...

  6. Android之分页加载数据

    基本的原理和我的上一篇随笔“Android之下拉刷新ListView”差不多,代码里面有注释,这里就不废话了,直接上代码. 自定义的分页显示ListView——PagedListView.java代码 ...

  7. Myeclipse不显示js文件错误的方法

    最近在学后台,用Myeclipse ,那么问题来了,Myeclipse 总是喜欢报 js 文件的错误,这就很烦了,看着也不舒服. 查看资料后,解决方法如下: 项目[鼠标右键] -> MyEcli ...

  8. c#生成二维码

            String link ="www.baidu.com";//这里一般是一个链接 封装后的方法,直接调用就可以了  public void CreateQRCode ...

  9. ubuntu14.04环境下spyder的安装

    在ubuntu14.04系统中,默认在/usr/lib目录下安装了python2.7.6和python3.4.3,在该环境下安装spyder,然后使其链接到python3.4.3. 首先安装为pyth ...

  10. 高程三:Array

    一:Array数组 1.Array.isArray(参数) 检测是否是数组,*不兼容IE8,兼容IE9及以上.Chrome.Firefox等,要兼容IE8,可以用 Object.prototype.t ...