问题描述:

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.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
  Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
  Total amount you can rob = 2 + 9 + 1 = 12.

思路:

动态规划问题。从头开始遍历每一个house,判断如果rob当前house和不rob当前house这两种情况下,能够获得的最大收益各是多少,并且记录下来;基于第i个house的结果记录,可以推断出第i+1个house的结果记录

代码:

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0 :#如果要是长度为0 ,则返回0
return 0
result = [nums[0],0]#result[0]代表rob nums[0]的结果,result[1]代表不rob nums[0]的结果
for i in range(1,len(nums)):#循环遍历,每次更新是以及否rob当前house的结果
#account_include_me = nums[i] + result[1]
# account_exclude_me = max(result)
#result = [account_include_me,account_exclude_me]
result = [nums[i] + result[1],max(result)]
return max(result)

Python3解leetcode Maximum SubarrayHouse Robber的更多相关文章

  1. Python3解leetcode Maximum SubarrayClimbing Stairs

    问题: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  2. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...

  3. Python3解leetcode Best Time to Buy and Sell Stock II

    问题描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  4. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  5. Python3解leetcode Rotate Array

    问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...

  6. Python3解leetcode Linked List Cycle

    问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...

  7. Python3解leetcode Single Number

    问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...

  8. Python3解leetcode Same TreeBinary Tree Level Order Traversal II

    问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  9. Python3解leetcode Same Tree

    问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

随机推荐

  1. pve之命令

    pvesr list ha-manager status pvecm nodes pvecm status pveperf qm list root@cu-pve06:~# pvesr list Jo ...

  2. Oracle--索引视图序列等对象

    ---恢复内容开始--- 索引 与表类似,不仅需要在DD中保存索引的定义,还需要在表空间为它分配实际的存储空间. 将索引和对应的表分别存放在不同硬盘的不同表空间中能够提高查询的速度,因为Oracle能 ...

  3. 28. Jmeter函数

    Jmeter函数传送门 软件测试汪简书地址 软件测试汪博客地址 欢迎关注微信公众号:软件测试汪.软件测试交流群:809111560 转载请注意出处,谢谢合作

  4. 【MM系列】SAP SAP的账期分析和操作

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP SAP的账期分析和操作   ...

  5. TensorFlow学习笔记1-入门

    TensorFlow学习笔记1-入门 作者: YunYuan *** 写在前面 本笔记是我学习TensorFlow官方文档中文版的读书笔记,由于尚未搭建好Github的个人博客的评论功能,故尚不方便与 ...

  6. Linux——管道与重定向

    参考资料: 极客学院IBMdeveloperWorks 重定向标准I/O Linux shell(比如Bash)接收或发送序列和字符串流形式的输入或输出.每个字符都独立于与之相邻的字符.字符没有被组织 ...

  7. linux操作系统的调度策略

    linux的进程分为两种 1.实时进程,优先级高,操作系统会优先执行这种进程 2.普通进程,大多数的进程都是这种进程 调度策略 unsigned int policy; 调度策略的定义 #define ...

  8. SpringMVC请求处理流程源码

    我们首先引用<Spring in Action>上的一张图来了解Spring MVC 的核心组件和大致处理流程: 从上图中看到①.DispatcherServlet 是SpringMVC ...

  9. SQL server 查看什么语句在使用临时表

    SQL server 查询那些语句在使用临时表 最近在日常的性能测试工作中发现,数据库端的IO读写比较大,有规律的2-8M的波动,数据库的版本为 SQL server 2008 sp3. 这些IO操作 ...

  10. hive 排序

    1.全局排序(order by) Order by:全局排序,只有一个reducer ASC(ascend):升序(默认) DESC(descend):降序 2.每个MR内部排序(sort by) s ...