Java for LeetCode 213 House Robber II
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 ...
随机推荐
- High Frequency Trading (整理中...)
什么是高频交易系统 1 交易指令完全是由电脑发送,对市场数据的响应延时在微秒级2 系统有专用的软硬件组成,研发时需要大量的计算机专家级的工作3 系统的硬件需要放在离交易所主机很近的位置,所谓co-lo ...
- Hadoop之HDFS文件操作常有两种方式(转载)
摘要:Hadoop之HDFS文件操作常有两种方式,命令行方式和JavaAPI方式.本文介绍如何利用这两种方式对HDFS文件进行操作. 关键词:HDFS文件 命令行 Java API HD ...
- Python学习手册(1入门知识-数据类型)
UNIX env查找技巧 在一些UNIX系统上,可以用这样一种方法避免硬编码Python解释器的路径,在文件的特定的第一行注释中写上这样一句话. #! usr/bin/env/ python...sc ...
- sql是如何执行一个查询的!
引用自:http://rusanu.com/2013/08/01/understanding-how-sql-server-executes-a-query/ Understanding how SQ ...
- jquery 获取datagrid行数
var data = $('#dg').datagrid('getData'); alert('总数据量:' + data.total)//注意你的数据源一定要定义了total,要不会为undefin ...
- [C++基础]关于对象的创建及内存分配
测试: #include <stdio.h>#include <QDebug> class KPoint{public: KPoint(int x, int y){ nx = ...
- post提交表单
<script type="text/javascript"> $(function () { $("#btnRefresh1").click(fu ...
- jQuery框架分析第一章: 第一个匿名函数
我的jQuery版本为1.7* 这个版本代码比之前的版本优化了很多,结构也清晰了不少,就用最新的吧. 打开jQuery源代码 首先你能看到所有代码被一个 (function(window,undefi ...
- 第26天 swift reflect
1.reflect http://www.jianshu.com/p/b5c87824e33c 2.android studio 2.0项目1.5编译 Cause: com/android/build ...
- codeblocks+Mingw 下配置开源c++单元测试工具 google test
google test 是google的c++开源单元测试工具,chrome的开发团队就是使用它. Code::Blocks 12.11(MinGW 4.7.1) (Windows版)Google T ...