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搜索到新节点时,都要保存该节点.而且每当找出一 ...
随机推荐
- javascript 【js‘s word】
http://mp.weixin.qq.com/s?__biz=MjM5MzY2NDY0Ng==&mid=214013689&idx=1&sn=21e03f6c7bf73893 ...
- UDP,TCP理解。
UDP: 面向无连接, 每个数据大小限制在64K内 因为面向无连接,所以就是不可靠协议. 将数据和源和谜底封装到数据包当中,不需要建立连接.速度快(就像送快递一样,管你在不可以先到你门口) 用处:聊天 ...
- ASP.NET Web API 通过参数控制返回类型(JSON|XML)
一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...
- Android - This Handler class should be static or leaks might occur.
今天学习了使用 HTTP协议,从Android客户端往Tomcat服务器端以GET发送请求,途中无意中发现自己写的Handler类被Android提示:This Handler class shoul ...
- 在.Net中进行跨线程的控件操作(上篇:Control.Invoke)
本文的重点在于介绍如何在多线程编程中,从非UI线程上访问界面中的控件.有过多线程编程经验的人都知道,当我们在非UI线程上试图给一个界面中的控件赋值的时候,比如说label的Text属性,系统会抛出一个 ...
- VS2008 未找到编译器可执行文件 csc.exe【当网上其他方法试玩了之后不起作用的时候再用这个方法】
被公司派遣到中国海洋石油惠州炼化公司做项目,做的是生产管理,来了发现他们的项目结构简直烂的要命,和同学们写的毕业设计差不多,然后开发工具用的是vs2008,我电脑是安装了vs2005和vs2010,v ...
- c#基础-----数据类型,转义字符,引用类型,类型转换
数据类型,转义字符,引用类型,类型转换 百度一下
- 连接远程LINUX服务器
远程登陆linux服务器需要下载一个软件,非常好用,名字是SecureCRT5,百度搜索有很多,如果下载不到可以联系我 运行安装包,一路下一步就可以了 安装好后,运行该软件 点击左上角第二 ...
- .NET中的委托——摘自MSDN
封装一个方法,该方法只有一个参数并且不返回值. 命名空间: System程序集: mscorlib(在 mscorlib.dll 中) 语法 C# public delegate void ...
- ES 必备插件的安装
1. elasticsearch-head插件的安装,非常好的插件 elasticsearch-head是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通 ...