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.
解题思路:
可以分解为两种情况:包含nums[0]和不包含nums[0],分别求解算出最大即可,JAVA实现如下;
public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
else if (nums.length <= 2)
return Math.max(nums[0], nums[nums.length - 1]);
else if(nums.length==3)
return Math.max(nums[0], Math.max(nums[1],nums[2]));
int res = 0;
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[2] = nums[0] + nums[2];
dp[3] = nums[0] + nums[3];
for (int i = 4; i < nums.length-1; i++)
dp[i] = nums[i] + Math.max(dp[i - 2], dp[i - 3]);
res=Math.max(dp[nums.length-2],dp[nums.length-3]);
dp[1] = nums[1];
dp[2] = nums[2];
dp[3] = nums[3] + nums[1];
for (int i = 4; i < nums.length; i++)
dp[i] = nums[i] + Math.max(dp[i - 2], dp[i - 3]);
return Math.max(res, Math.max(dp[nums.length-1],dp[nums.length-2]));
}
Java for LeetCode 213 House Robber II的更多相关文章
-
[LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
-
[LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
-
LeetCode 213. House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
-
[leetcode] #213 House Robber II Medium (medium)
原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...
-
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:二叉树下的不能相邻,求能 ...
-
198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
-
【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
-
【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
-
Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
随机推荐
-
Java2OP
Java2OP D:\Program Files (x86)\Embarcadero\Studio\18.0\bin\converters\java2op\Java2OP.exe Java2OP.ex ...
-
Microsoft Mole原理及常见问题整理
Moles与Moq(Rhino.Mocks)比较 作用范围 Moq与Rhino.Mocks这类的Mock是对Interface或AbstractClass做Mock, 而Moles是Mock整个 ...
-
iOS--九宫格布局
[self rankWithTotalColumns: andWithAppW: andWithAppH:]; //九宫格布局 - (void)rankWithTotalColumns:(int)to ...
-
Android listview 的应用
ListView作为Android最常用但是却最难用的控件之一,有很多神奇的用法.我之前也有写过一个例子,稍微不那么简单了一点. [Android原生item的伸缩效果]:http://www.cnb ...
-
java中如何获得方法中的参数名
在反射的时候我们可以通过class的getParameterNames()反射获得参数的名称,但是这个名称并不是参数的真实名称,而是类似于arg0,arg1等占位名称. 下面介绍一种方法获得参数真实名 ...
-
linux远程ssh一键设置服务器时间
cmd="sudo date -s \"$1\""; ssh mrdTomcat@*.*.*.* "$cmd" 是不是遇到过很多问题 ssh ...
-
数据分析面试题之Pandas中的groupby
昨天晚上,笔者有幸参加了一场面试,有一个环节就是现场编程!题目如下: 示例数据如下,求每名学生(ID)对应的成绩(score)最高的那门科目(class)与ID,用Python实现: 这个题目 ...
-
2019 校内赛 RPG的地牢猎手(bfs+优先队列)
Problem Description Luke最近沉迷一款RPG游戏,游戏中角色可以进入地牢关卡,只要顺利走出地牢就可以获得奖励.地牢表示为n行m列的块矩阵,其中每个块只可以是障碍块.入口.出口或数 ...
-
正则表达式中,[\s\S]* 什么意思
https://blog.csdn.net/haoyuedangkong_fei/article/details/53781936 例如:[a-z]表示从a到z之间的任意一个. 不是这样的吗?谁能给我 ...
-
ovs flow 原理及实验
OpenFlow概述 在支持OpenFlow的交换机中包含了若干个Flow table,Flow table可以用来控制数据包的处理,交换机会执行与flow相匹配的表项中所罗列的动作. OpenFlo ...