问题描述:

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. random——伪随机数生成模块

    random——伪随机数生成模块 转自:https://blog.csdn.net/zhtysw/article/details/79978197 该模块包含构造伪随机数生成器的多个方法.对于整数,伪 ...

  2. 使用 Select2 下拉框实现复选

    使用 Select2 下拉框实现复选 <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...

  3. Microsoft Azure_Fabric

    目录 目录 前言 Microsoft Azure Microsoft Azure Fabric Controller 前言 WindowsAzure是相对于全球版Microsoft Azure而言的中 ...

  4. Ubuntu下apt-get 安装apache2、php、mysql后的默认路径

    apache: 采用apt-get 在线安装,安装路径应在/etc/apache2目录下 apache配置文件/etc/apache2/apache2.conf Apache模块路径:/usr/sbi ...

  5. 不同vlan之间相互通信

    不同VLAN之间相互通信的两种方式 (单臂路由.三层交换) 试验目的: 1.通过单臂路由实现不同VLAN之间的通信 2.通过三层交换路由功能实现不同VLAN之间的通信   网络拓扑图: 1.单臂路由实 ...

  6. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

  7. vsCode python的函数跳转功能失效修复

    经常遇到VsCode失效的问题,很有可能是插件的原因. 关闭插件提示功能 "python.pythonPath": "/usr/bin/python", 关闭所 ...

  8. 命令行打包war包

    输入jar -cvf  包名.war  目录/*

  9. P5030 长脖子鹿放置

    题目背景 众周所知,在西洋棋中,我们有城堡.骑士.皇后.主教和长脖子鹿. 题目描述 如图所示,西洋棋的"长脖子鹿",类似于中国象棋的马,但按照"目"字攻击,且没 ...

  10. luoguP1311 选择客栈 题解(NOIP2011)

    P1311 选择客栈  题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<c ...