【leetcode】House Robber
题目简述
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的更多相关文章
- 【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 ... 
- 【LeetCode】House Robber III(337)
		1. Description The thief has found himself a new place for his thievery again. There is only one ent ... 
- 【LeetCode】树(共94题)
		[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ... 
- 【LeetCode】Minimum Depth of Binary Tree   二叉树的最小深度 java
		[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ... 
- 【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 ... 
- 53. Maximum Subarray【leetcode】
		53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ... 
- 27. Remove Element【leetcode】
		27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ... 
- 【刷题】【LeetCode】007-整数反转-easy
		[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ... 
- 【刷题】【LeetCode】000-十大经典排序算法
		[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法 
随机推荐
- django创建新项目anministrator问题
			1.app中models的class可以通过migrations命令生成相应的数据表 2.此时并未写入数据库,migrate命令可以把相应的改动更新到数据库中 3.createsuperuser命令创 ... 
- MyBatis源码分析(5)——内置DataSource实现
			@(MyBatis)[DataSource] MyBatis源码分析(5)--内置DataSource实现 MyBatis内置了两个DataSource的实现:UnpooledDataSource,该 ... 
- jquery复选框checkbox实现删除
			实现这样一个基本需求,页面有很多数据,可以删除一条或多条,删除前判断是否选中至少一条,否则提示. function deleteUser() { //当时是想把内容以str+="" ... 
- ASP.NET MVC和EF集成AngularJS开发
			参考资料: 如何在ASP.NET MVC和EF中使用AngularJS AngularJS+ASP.NET MVC+SignalR实现消息推送 [AngularJs + ASP.NET MVC]使用A ... 
- 【10-26】java调试技术学习笔记
			调试工具 jdk自带的工具 jmap jconsole VisualVM jmap jmap -histo:live pid 列出该进程的所有活动实例统计信息 jmap -dump:live,file ... 
- python3.5.2本地环境搭建
			OS:win7 Download URL:https://www.python.org/downloads/release/python-352/ install 下载二进制安装文件之后,点击安装,一 ... 
- 无意之间发现的Servlet3.0新特性@WebServlet
			今天无意之间看到了一个注解,这个注解就是@WebServlet,@WebServlet 用于将一个类声明为 Servlet,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为 Se ... 
- VB操作EXCEL文件
			用VB操作Excel(VB6.0)(整理) 首先创建Excel对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel. ... 
- [BZOJ1014][JSOI2008]火星人prefix
			[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ... 
- yii2——自定义widget
			参考资料:http://www.bsourcecode.com/yiiframework2/how-to-create-custom-widget-in-yii2-0-framework/ 如何使 ... 
