House Robber II 解答
Question
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.
Solution
Key to this problem is to break the circle. So we can consider two situations here:
1. Not include last element
2. Not include first element
Therefore, we can use the similar dynamic programming approach to scan the array twice and get the larger value.
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length < 1)
return 0;
int length = nums.length, tmp1, tmp2;
if (length == 1)
return nums[0];
int[] dp = new int[length];
dp[0] = 0;
dp[1] = nums[0];
// First condition: include first element, not include last element;
for (int i = 2; i < length; i++)
dp[i] = Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]);
tmp1 = dp[length - 1];
// Second condition: include last element, not include first element;
dp = new int[length];
dp[0] = 0;
dp[1] = nums[1];
for (int i = 2; i < length; i++)
dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]);
tmp2 = dp[length - 1];
return tmp1 > tmp2 ? tmp1 : tmp2;
}
}
House Robber II 解答的更多相关文章
- [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 ...
- [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 ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
随机推荐
- UESTC_排名表 2015 UESTC Training for Graph Theory<Problem I>
I - 排名表 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- JS帮你计算属相
背景:一个人出生在2014年的正月初一,他的生肖到底是属蛇还是属马呢?这就要确定那一天才是一年的开始.是春节还是立春?每年的春节是正月初一,但是生肖必须是从立春日开始计算.春节是1912年孙中 ...
- python标准库 bisect模块
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #bisect #作用:维护有序列表,而不必在每次向列表增加一个元素 ...
- 机房收费系统合作版(三)——UI思索
案件追踪系统1.0暂告一段落.验收过程中.MR MI针对UI界面提出了很多自己的想法. 针对TGB项目的UI设计我也有我的感受: 1.不论大小项目.仅仅要一看界面准有70%到80%熟悉度. 2.一看这 ...
- 零拷贝概念 -- linux内核
零拷贝(zero-copy) 备快速网络接口的主要技术. 零拷贝技术通过降低或消除关键通信路径影响速率的操作,降低传输数据的操作系统开销和协议处理开销,从而有效提高通信性能,实现快速传输数据. 零拷贝 ...
- 基于阿里云server搭建SVNserver
基于阿里云server搭建SVNserver 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/ex_net/article/details/8577 ...
- Linq GroupJoin 使用
备忘: var data = BoshccEntities.Current.TB_MB_1 .GroupJoin(BoshccEntities.Current.TB_MB_2, o => o.H ...
- 导出word文档
string id = Request["id"]; if (string.IsNullOrEmpty(id)) { ...
- 配置元素customErrors
Asp.net配置文件的配置方式,其实在MSDN里面是写得最清楚的了.可惜之前一直未曾了解到MSDN的强大. 先贴个地址:http://msdn.microsoft.com/zh-cn/library ...
- PHP学习笔记二十一【全局变量】
<?PHP //定义全局变量 global $a; $a=9; //给全局变量赋值 function test1() { global $a; $a=45; } test1(); echo $a ...