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 ...
随机推荐
- centos安装docker
一.升级内核 [root@iZ2893wjzgyZ ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org [root@iZ289 ...
- 配置Tomcat使用Redis作为session管理
1. 在 tomcat/lib 中增加以下jar包 commons-pool2-.jar jedis-.jar tomcat-redis-session-manager-.jar 2. 修改tomca ...
- npm淘宝镜像
前端开发会用到npm的包,但是国外的速度有时候很慢,幸运的是,淘宝做了镜像,一起来看看吧. https://npm.taobao.org/
- 重构Web Api程序(Api Controller和Entity) 续篇(1)
经过一系列重构,你眼明的话,还是可以看到还有重构的地方,如: string newFileName = "~/Temp/" + Guid.NewGuid().ToString() ...
- [转]各种有用的PHP开源库精心收集
FROM : http://my.oschina.net/caroltc/blog/324024 1.html2ps and html2pdf 下载地址: http://www.tufat.co ...
- mybatis3.2.8 与 hibernate4.3.6 混用
mybatis.hibernate这二个框架各有特色,对于复杂的查询,利用mybatis直接手写sql控制起来更灵活,而一般的insert/update,hibernate比较方便.同一个项目中,这二 ...
- climits
<climits>头文件定义的符号常量 CHAR_MIN char的最小值SCHAR_MAX signed char 最大值SCHAR_MIN signed char 最小值UCH ...
- Qt 5.2 Creator 和 vs2012 QT 插件的安装
在QT官网下载QT http://qt-project.org/downloads 我下的是64位版本Qt 5.2.1 for Windows 64-bit vs2012插件是 Visual Stu ...
- StackExchange.Redis--纯干货喂饱你
Redis和StackExchange.Redis redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库 ...
- unity3d 音频无缝循环
在我做赛车漂移的时候,漂移的声音断断续续的,搞得我很郁闷 大家可以随便找个音效然后循环播放去仔细听 你会发现当音效播放完成一次之后循环播放第二次时会停顿一下 我做赛车漂移如果中途停顿了体验是非常不好的 ...