leetcode日记 HouseRobber I II
House Robber I
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.
大意:你是一个专业盗贼,将要抢劫一条街上的房子,每个房子都有一定的钱可以抢,唯一能阻止你抢劫的是警报器,如果相邻的两个房子被抢,那么警报器就会触发。
现在给你一个非负的数组代表每个房子的金额,求出如何能够抢劫到最大数额的钱而不触发警报。
分析:不知道为什么,看到这个题很明显的就可以向动态规划上靠,感觉很像上楼梯和斐波那契数列那种,因此就向这个方向想想,假设数组有五个元素,那么对于能够抢到的最多的钱,一定是 第五个元素的金额加上前三个元素组成的数列所求的结果 与 前四个元素所组成元素所求结果 这两个结果中比较大的那个,那么这样子5个元素数列问题就转化成 4个元素数列问题和3个元素数列问题。以此类推,只要知道一个元素数列问题的结果和两个元素数列问题的结构就可以依次推出之后的结果。复杂度:时间上,线形时间内完成,只需要遍历一次。空间上,线形空间内完成,只需要额外的一个和原数组大小相同的数组。
- class Solution(object):
- def rob(self, nums):
- lens=len(nums)
- if lens==0:
- return 0
- if lens==1:
- return nums[0]
- if lens==2:
- return max(nums)
- result=[]
- result.append(nums[0])
- result.append(max(nums[:2]))
- for i in xrange(2,lens):
- tem=max(result[i-1],result[i-2]+nums[i])
- result.append(tem)
- return result[-1]
House Robber II
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
大意:这是上一题的扩展
这一次街道变成环状街道,警报触发模式一样,这一次怎样抢劫到最多的钱。
分析:最直接的思路为将这个环形街道变成线形街道,这样就可以使用上一题的模式进行解答。根据此思路进行分析,同样假设5个元素,同样如果要抢到最多的钱,那么一定是 第五个元素加上二三元素(第一第四元素相邻第五元素,因此除去)组成的数列问题的结果 与 前四元素组成的数列问题的结果 这两结果中比较大的即为答案。那么这样就将原本的环形问题转化为两个线形问题。这样即可得到答案。复杂度:将一个环形问题拆成两个线形问题,因此时间上复杂度为O(2n),依然是线性时间,空间上重用同一条数组的话依然只需要O(n)的空间。
- class Solution(object):
- def rob1(self,nums):#House Robber I的解法
- lens=len(nums)
- if lens==0:
- return 0
- if lens==1:
- return nums[0]
- if lens==2:
- return max(nums)
- result=[]
- result.append(nums[0])
- result.append(max(nums[:2]))
- for i in xrange(2,lens):
- tem=max(result[i-1],result[i-2]+nums[i])
- result.append(tem)
- return result[-1]
- def rob(self,nums):
- lens=len(nums)
- if lens==1 or lens ==3 or len==2:
- return max(nums)
- if lens==0:
- return 0
- line1=nums[:lens-1]
- line2=line1[1:lens-2]
- tem1=self.rob1(line1)
- tem2=self.rob1(line2)
- return max(tem1,nums[-1]+tem2)
leetcode日记 HouseRobber I II的更多相关文章
- python leetcode 日记 --Contains Duplicate II --219
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
随机推荐
- (转)Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph.
Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph 2019-04-27 09:33:58 This ...
- [luaj]在安卓用使用luaj
luaj与安卓 什么是luaj luaj是一个Java的一个lua语言引擎,他可以让你在java上运行Lua代码. 在安卓中使用lua干嘛 lua代码可以用来书写布局,或者一些业务逻辑. 为什么要在安 ...
- Cent OS 7 安装海峰、极点五笔输入法
作为五笔输入法的玩家,输入不使用五笔比较难受:最近安装了 Cent OS 7 (带 GNOME. KDE桌面)系统,默认安装的是拼音输入法,这那受得了,赶紧上车找五笔输入法. 在此之前我查阅了百度得到 ...
- 解决问题:怎样在页面获取数组和List集合的长度
解决问题:怎样在页面获取数组和List集合的长度 我们在前端遍历后台数据的时候,经常是从后台传过来一个数组或List集合,在前端页面就可以使用JSTL的<c:For each>标签遍历数据 ...
- error CS1002: ; expected 错误解决
一般出现这种错误,大概原因是因为前端页面里的C#代码少个分号,或少个括号 导致编译器出错:仔细检查页面中的C#代码是否写的正确. 我之所以出现这个错误是因为前台页面中:@{ } 这里的代码少一个括号 ...
- 承接VR外包,虚拟现实外包,北京正规公司
我们制作各类型VR全景虚拟现实,增强现实视频制作.录制等项目.品质保证,售后完备,可签合同.contectus: 13911652504(技术经理tommy) 承揽VR外包 虚拟现实外包 U3D外包( ...
- Kotlin 类和对象
类定义 Kotlin 类可以包含:构造函数和初始化代码块.函数.属性.内部类.对象声明. Kotlin 中使用关键字 class 声明类,后面紧跟类名: class Runoob { // 类名为 R ...
- Python 开发 项目《外星人入侵》
2019-02-05 本篇心路历程: 本篇是打算记录自己的第一个python项目,也是众人皆知的<外星人入侵项目>,本项目大概500多行.趁着寒假,大概耗时3天吧,把完整代码敲了出来,当然 ...
- java异常处理try catch finally
1 异常 1.1 异常处理的作用 在编程时,如果出现文件打开失败,读写文件就会异常退出.如果出现内存溢出错误,程序也会异常退出.如果不能对这些异常进行处理.程序则无法正常运行.所 ...
- PyQt5——隐藏控件并保留位置
原文地址:https://blog.csdn.net/qq_38161040/article/details/86605798 ———————————————————————————————— 设置控 ...