问题描述:

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. 牛客2019提高D1t1 最短路

    分析 我们发现可以按照ai从小到大排序 边的大小就是当前的a减去前面第一个不等于它的a 代码 #include<iostream> #include<cstdio> #incl ...

  2. JS-DOM Event

    DOM Level 0 Events:绑定到 DOM 的属性上,找不到官方文档 DOM0 是在 W3C 进行标准备化之前出现的,实际上是未形成标准的试验性质的初级阶段的 DOM. var tdiv = ...

  3. vue中的导航钩子

    //钩子 登录拦截 router.beforeEach((to, from, next) => { const sessionToken = window.sessionStorage.getI ...

  4. Bootstrap 学习笔记6 列表组面板嵌入组件

    列表组组件: 面板组件:

  5. 校内模拟赛T5:连续的“包含”子串长度( nekameleoni?) —— 线段树单点修改,区间查询 + 尺取法合并

    nekameleoni 区间查询和修改 给定N,K,M(N个整数序列,范围1~K,M次查询或修改) 如果是修改,则输入三个数,第一个数为1代表修改,第二个数为将N个数中第i个数做修改,第三个数为修改成 ...

  6. CentOS7和Ubuntu18.10下运行Qt Creator出现cannot find -lGL的问题的解决方案

    解决方法:缺少相应的opengl的库,需要安装opengl库 一.Ubuntu下解决Qt5.11.1 cannot find -lGL 有两种原因: 一种是没有按照libGL库,那么就安装: sudo ...

  7. c.vim

    放在 /usr/share/vim/vim80/syntax/c.vim 最后: syn match cFunctions "\<[a-zA-Z_][a-zA-Z_0-9]*\> ...

  8. vue基础介绍及使用

    1.vue基本使用 vm:el / data v:v-model / {{ }} 1. 引入Vue.js 2. 创建Vue对象 el : 指定根element(选择器) data : 初始化数据(页面 ...

  9. Anaconda Jupyter WinError2:The system cannot find the file specified

    Traceback (most recent call last): File "C:\Users\builder\Miniconda3\Scripts\conda-build-script ...

  10. CodeChef A String Game(SG)

    A String Game   Problem code: ASTRGAME   Submit All Submissions   All submissions for this problem a ...