Python3解leetcode Maximum SubarrayHouse 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.
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的更多相关文章
- 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 ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- 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 ...
- 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 ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- 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 ...
- Python3解leetcode Single Number
问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- 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 ...
- Python3解leetcode Same Tree
问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
随机推荐
- leetcode 230. 二叉搜索树中第K小的元素(C++)
给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [ ...
- Jenkins+Gitlab+自动化测试配置持续集成
Jenkins安装在win7上 GitLab安装在docker上 需求:本地提交自动化测试代码在gitlab上后,jenkins自动构建,拉下新提交的自动化代码,并且运行 参考的链接: https:/ ...
- 移动端安全 - 安卓Android - 工具相关
渗透工具 drozer .安装文件解压后文件介绍 setup.exe ---安装 agent.apk ---用于调试 - 安装在安卓手机上 使用命令 . cd 到 drozer 安装目录 . adb ...
- Maven系列学习(一)Maven基本知识
Maven 简介 1.Maven主要是基于Java平台的项目构建,依赖管理和项目信息 2.Maven是优秀的构建工具,跨平台,消除构建的重复,抽象了一个完整的构建生命周期模型,标准化构建过程 3.管理 ...
- 09 (H5*) JS第7天 原型
目录 1:创建对象的3中方式 2:工厂模式创建实例对象 3: 实例对象和构造函数的关系 4:构造函数创建对象带来的问题--原型 5:原型中创建方法 6:构造函数.原型对象.实例对象的关系 7:原型对 ...
- luoguP2123 皇后游戏(贪心)
luoguP2123 皇后游戏(贪心) 题目 洛谷题目chuanso 题解 有一篇好题解,我就懒得推式子了,毕竟打到电脑上还是很难的 牛逼题解传送门 code #include<iostream ...
- P2579 [ZJOI2005]沼泽鳄鱼(矩乘)
P2579 [ZJOI2005]沼泽鳄鱼 没有食人鱼:直接矩乘优化 有食人鱼:食人鱼周期2.3.4,公倍数12,可以以12为一个周期矩乘,剩下的暴力 注意矩乘不满足乘法交换律,一定要注意乘的顺序 #i ...
- JavaScript中的map()函数
概述Array.map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,同时不会改变原来的数组. 用法 Array.map(callback); 示例 //简单数组 const ...
- elasticsearch 深入 —— 近似匹配
近似匹配 使用 TF/IDF 的标准全文检索将文档或者文档中的字段作一大袋的词语处理. match 查询可以告知我们这大袋子中是否包含查询的词条,但却无法告知词语之间的关系. 思考下面这几个句子的不同 ...
- shell根据系统当前的时间向用户输出问候信息