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 ...
随机推荐
- unix c 05
dup和dup2用于复制文件描述符,区别在于dup2可以指定新的文件描述符的数值,如果新的文件描述符的值已经被使用,dup2会关闭掉后进行复制. dup和dup2 不会复制文件表,只是文件描述符的复制 ...
- JS--回到顶部代码
原文地址:http://www.cnblogs.com/liguiqiang1986/articles/3132023.html JS--回到顶部代码 <!DOCTYPE html PUBLIC ...
- Sql Server 2005的1433端口打开和进行远程连接
参考地址:http://topic.csdn.net/u/20090828/16/e693935a-99b7-4090-a6bc-0123c91183eb.html 1.如何打开sql server ...
- 烟雾检测笔记1--《Video-based smoke detection with histogram sequence of LBP and LBPV pyramids》解析、实现
基于HEP(histograms of equivalent patterns[1])框架下的特征具有良好的纹理分类效果,LBP(local binary patterns[2])属于HEP框架下最常 ...
- php创建带logo的二维码
<?php /** php使用二维码 **/ class MyQrcode{ const SIZE = 150; const LEVEL = "L"; const MARGI ...
- 【HDU】 1018 Big Number
大意就是求 : log10(n!) = log10(1 * 2 * 3 * .......*n) = log10(1) + log10(2) + ........+log10(n); 打表的话会ML ...
- [core java学习笔记][第六章接口与内部类]
接口域内部类 接口 描述类具有什么功能,不给出具体实现. 内部类 用于设计协作关系的类集合 代理 实现任意接口的对象. 6.1 接口 接口声明 public interface Comparable ...
- asp.net MVC 学习笔记
1.可以看出每个区域Areas里都是个mini的MVC项目,Controller.Models.Views一个都不缺,还多了一个AdminAreaRegistration类 2.MVC 将URL映射到 ...
- Android开发:shape和selector和layer-list的(详细说明)
http://blog.csdn.net/brokge/article/details/9713041
- log4j参数说明
log4j.properties 使用 一.参数意义说明 输出级别的种类 ERROR.WARN.INFO.DEBUG ERROR 为严重错误 主要是程序的错误 WARN 为一般警告,比如session ...