Leetcode House Robber II
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.
本题和House Robber差不多,分成两种情况来解决。第一家是不是偷了,如果偷了,那么最后一家肯定不能偷。
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
n = len(nums)
if n == 1:
return nums[0]
if n == 2:
return max(nums[0], nums[1]) dp = [0]* n
dp[0] = 0
dp[1] = nums[1] for i in range(2,n):
dp[i] = max(dp[i-2]+nums[i], dp[i-1]) case1 = dp[-1] dp = [0]* (n-1)
dp[0] = nums[0]
dp[1] = nums[0] for i in range(2,n-1):
dp[i] = max(dp[i-2]+nums[i], dp[i-1]) case2 = dp[-1] return max(case1, case2)
Leetcode House Robber II的更多相关文章
- [LeetCode]House Robber II (二次dp)
213. House Robber II Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- LeetCode之“动态规划”:House Robber && House Robber II
House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...
随机推荐
- Javascript备忘复习笔记2
一.函数与形参 1.函数 function abs(x) { if (x >= 0) { return x; } else { return -x; } } alert(abs(-10)); 2 ...
- iOS多线程杂论
iOS多线程的分布 (1) NSThread (2) NSOperation (3) GCD 现在对下面三个进行一个个的分析,希望那里说得不对的地方希望简友们帮我指点一二. 1,NSThread 优点 ...
- make的控制函数(error,warning)
make的控制函数 make提供了两个控制make运行方式的函数.通常它们用在Makefile中,当make执行过程中检测到某些错误是为用户提供消息,并且可以控制make过程是否继续. 8.11.1 ...
- codevs 3165 爱改名的小融2
3149 爱改名的小融 2 http://codevs.cn/problem/3149/ 题目描述 Description Wikioi上有个人叫小融,他喜欢改名.现在他的要求变了,只要是英文字母就是 ...
- vtk renderer / rendering 绘制
1.在绘制窗口中绘制出物体(静态的)vtkRenderWindow * w=vtkRenderWindow::New(); w->AddRenderer(r); for(int ...
- caffe的python接口学习(2):生成solver文件
caffe在训练的时候,需要一些参数设置,我们一般将这些参数设置在一个叫solver.prototxt的文件里面,如下: base_lr: 0.001 display: 782 gamma: 0.1 ...
- 利用windbg查找dictionary导致IIS占CPU100%案例分析(一)
一.背景 先说下windbg使用场景.各位coder在工作中或多或少都会遇到下面四种情况 1.本地代码好好的,放服务器上运行一段时间后,IIS服务突然占用 w3wp.exe CPU突然100% ,不得 ...
- unity3d Vector3.Lerp解析
Vector3.Lerp:http://www.ceeger.com/Script/Vector3/Vector3.Lerp.html 手册中描述的不是很详细,什么叫“按照数字t在from到to之间插 ...
- IIS安装与MVC程序部署
最近在做访客系统,虽然说不是什么多大的项目,但麻雀虽小五脏俱全,使用EF Code First+Mysql+Frozenui响应式布局,感觉通过这个项目学到好多东西,Mysql的使用.EF映射Mysq ...
- 微信公众平台SDK
微信公众平台网址:https://mp.weixin.qq.com/ 服务号说明:给企业和组织提供更强大的业务服务与用户管理能力,帮助企业快速实现全新的公众号服务平台. .NETSDK: Loogn. ...