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 ...
随机推荐
- 【转】多核CPU运行模式
多核CPU运行模式主要有以下三种: •非对称多处理(Asymmetric multiprocessing,AMP)——每个CPU内核运行一个独立的操作系统或同一操作系统的独立实例(instantiat ...
- Linux 文件系统同步
同步就是将物理内存中dirty的页写入到磁盘中,保证磁盘和物理页之间的内容一致. 触发同步操作的时机: 1.周期性内核线程,扫描脏页,根据一定的规则选择脏页,将页写回到磁盘. 2.如果内核中的脏页过多 ...
- ssh登录失败处理步骤
如果登录失败而又找不到显示的原因,优先使用ssh -vT name@ip -p port 进行调试,查看所使用的key文件.ip.端口是否正确.然后再检查下面步骤:1.检查在对应用户名下是否有iden ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- 汉字与utf8相互转化
NSString* strA = [@"%E4%B8%AD%E5%9B%BD"stringByReplacingPercentEscapesUsingEncoding:NSUTF8 ...
- python学习之路-2 初识python数据类型
数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值范围为-2** ...
- SQL参数化
本文来自:caodonglin 一.SQL参数化为什么能防注入? 因为执行计划被重用了,所以可以防SQL注入. 下面有两段SQL 正常SQL: 1 select COUNT(1) from C ...
- strut2 自己定义文件上传错误信息
在文件上传过程中我们能够指定拦截器对文件类型.后缀名.大小进行设定,action中的配置: <interceptor-ref name="fileUpload"> &l ...
- 利用Python进行数据分析——数据规整化:清理、转换、合并、重塑(七)(1)
数据分析和建模方面的大量编程工作都是用在数据准备上的:载入.清理.转换以及重塑.有时候,存放在文件或数据库中的数据并不能满足你的数据处理应用的要求.很多人都选择使用通用编程语言(如Python.Per ...
- EffectiveC#8--确保0对于值类型数据是有效的(初始化问题)
1.决不要创建一个不包括0在内的枚举类型 2.举例如下: public enum Planet { Mercury = 1, Venus = 2, Earth = 3, Mars = 4, Jupit ...