House Robber II——Leetcode
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.
题目大意:所有的住户围成一圈,只能隔着抢劫,问可以抢到的总额最大是多少。
解题思路:
因为第一家和最后一家是连着的,所以抢第一家就不能抢最后一家,抢最后一家就不能抢第一家,那么可以把数组分成两段,0~n-1和1~n两段,分别计算最大的,取较大的。
public int rob(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
if(nums.length==1){
return nums[0];
}
if(nums.length==2){
return Math.max(nums[0],nums[1]);
}
int res = -1;
int[] max = new int[nums.length+1];
max[0]=nums[0];
max[1]=Math.max(nums[0],nums[1]);
for(int i=2;i<nums.length-1;i++){
max[i]=Math.max(max[i-1],max[i-2]+nums[i]);
}
res=Math.max(res,max[nums.length-2]);
Arrays.fill(max,0);
max[1]=nums[1];
max[2]=Math.max(nums[1],nums[2]);
for(int i=3;i<nums.length;i++){
max[i]=Math.max(max[i-1],max[i-2]+nums[i]);
}
res=Math.max(res,max[nums.length-1]);
return res;
}
House Robber II——Leetcode的更多相关文章
- Housse Robber II | leetcode
可以复用house robber的代码,两趟dp作为两种情况考虑,选最大值 #include <stdio.h> #define MAX 1000 #define max(a,b) ( ( ...
- [LeetCode]House Robber II (二次dp)
213. House Robber II Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...
- 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 ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
随机推荐
- H TML5 之 (5) 一个在线画图板
这里加入了点难度了,增加了对HTML很多时间的把握,对象的把握 index.html <!DOCTYPE HTML> <html> <head> <title ...
- SQL内外左右交叉连接
什么是连接查询? 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据. 目的:实现多个表查询操作. 一般是用作关联两张或两张以上的数据表时用的.看起来有点抽象,我们举个例子,做两张表:学生表( ...
- error: Unable to find vcvarsall.bat while install python library by pip install or python setup.py install.
Python 2.7 会搜索 Visual Studio 2008. 如果你电脑上没有这个版本的话,比如只有: 1.Visual Studio 2010,在cmd里面执行:SET VS90COMNTO ...
- oracle 消除块竞争(hot blocks)
上篇日志提到了,那么高的负载,是存在数据块读竞争,下面介绍几个方法来消除块竟争 查找块竟争 SELECT p1 "file#", p2 "block#", p3 ...
- java输入输出流(内容练习)
1,编写一个程序,读取文件test.txt的内容并在控制台输出.如果源文件不存在,则显示相应的错误信息. package src; import java.io.File; import java.i ...
- 【转】发布iOS应用程序到苹果APP STORE完整流程
原文: http://www.cnblogs.com/JuneWang/p/3850859.html 可以为每个app上传5张截图,虽然至少需要上传一张,可能很少有人会只上传一张图片.另外,你还需要分 ...
- PLSQL常用时间函数
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- PHP设计模式之:工厂模式
工厂模式: 由工厂类根据参数来决定创建出哪一种产品类的实例: 工厂类是指包含了一个专门用来创建其他对象的方法的类.所谓按需分配,传入参数进行选择,返回具体的类.工厂模式的最主要作用就是对象创建的封装. ...
- 开发错误日志之IllegalArgumentException:MALFORMED
java.lang.IllegalArgumentException: MALFORMED 上面存在中文问题是因为java.util.zip下的格式转换有问题 ,jdk中的zip存在字符编码的问题. ...
- php5.4安装ecshopphp5.4问题及解决
includes/cls_template.php line422 将 $tag_sel = array_shift(explode(" ", $tag)); 这句话拆开为两句. $tag_exp = ...