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 ...
随机推荐
- Skyline(6.x)-Web二次开发-多窗口对比
GitHub 上获取源码 1. 打开个 3D 窗口 一个页面加载多个 TerraExplorer3DWindow 和 SGWorld 等只有第一个能用(即使用 iframe 也是一样) 所以我决定打开 ...
- pandas向左移动非空单元格
碰到个小问题, 因为需要更改excel 的行为. 处理最多的还是excel的事, 不过看起来是excel.实际上对pandas 的库的熟悉.读取,更改,应用等等,方法. 十分有用. 先说需求,本来的表 ...
- JSP 学习笔记1
JSP 学习笔记 JSP是Servlet的一种特殊形式,每个JSP页面就是一个Servlet实例--JSP页面有系统编译成Servlet,Servlet再负责响应用户请求. 1.JSP注释 < ...
- HTML--JS 获取选择框信息
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- nodejs基础-nvm和npm
nvm npm 更新 npm install npm@latest -g 本地安装 npm install 包名称 require(”包名“) 全局安装 npm install 包名 -g 可以直接作 ...
- vue手写轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- opencv2——直方图5
(一)图像直方图 图像的构成是有像素点构成的,每个像素点的值代表着该点的颜色(灰度图或者彩色图).所谓直方图就是对图像的中的这些像素点的值进行统计,得到一个统一的整体的灰度概念.直方图的好处就在于可以 ...
- lucene版本升级到4.6.0以上之后使用ik分词器遇到的问题
在将lucene core版本从4.5.1升级到4.7.0后,如下代码使用ik分词器报错 IKAnalyzer analyzer = new IKAnalyzer(true); StringReade ...
- [原]__ASSEMBLY__的用途
在Linux Kernel中有些constant需要被C code 跟 assembler共同使用 在用constant的時候,不能單方面給0x1000UL因為assembler無法看這東西. 但是C ...
- pyspider启动错误解决(Python 3.7)
问题一 安装好pyspider之后,在启动的时候,报出上图错误. 原因 async和await从 python3.7 开始已经加入保留关键字中. 参考: What’s New In Python 3. ...