House Robber II
https://leetcode.com/problems/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.
解题思路:
前面一题的follow-up,多了一个限制条件,第一个和最后一个房子,也算相邻,所以只能偷一个。这样在计算到dp[nums.length]的时候,不能再简单的max(dp[i - 2] + nums[i], dp[i - 1])了。前面的dp[i - 2]不能包含第一个房子。问题是,你不知道这个dp[i-2]是不是偷了第一个房子,因为子状态的定义是,偷到第i个房子时候的最大值。
所以,这题可以分解为两个问题,1)偷第一个房子,时候的最大值,2)偷最后一个房子,时候的最大值。
dp两次,再求最大值。
public class Solution {
public int rob(int[] nums) {
if(nums.length == 0) {
return 0;
}
if(nums.length == 1) {
return nums[0];
}
int prepre = 0;
int pre = 0;
int result = pre;
int max1 = 0;
for(int i = 0; i < nums.length - 1; i++) {
result = Math.max(pre, prepre + nums[i]);
prepre = pre;
pre = result;
}
max1 = result; int max2 = 0;
prepre = 0;
pre = 0;
result = pre; for(int i = 1; i < nums.length; i++) {
result = Math.max(pre, prepre + nums[i]);
prepre = pre;
pre = result;
}
max2 = result;
return Math.max(max1, max2);
}
}
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 ...
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- Leetcode House Robber II
本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...
- 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 ...
随机推荐
- Redbean:入门(三) - Exec 以及 Query 以及 ConvertToBeans
<?php //引入rb入口文件 include_once 'rb.php'; //定义dsn以及相关的数据 $dsn = 'mysql:host=localhost;dbname=hwibs_ ...
- System.IO之内存映射文件共享内存
内存映射文件是利用虚拟内存把文件映射到进程的地址空间中去,在此之后进程操作文件,就 像操作进程空间里的地址一样了,比如使用c语言的memcpy等内存操作的函数.这种方法能够很好的应用在需要频繁处理一个 ...
- ios多线程和进程的区别(转载)
很想写点关于多进程和多线程的东西,我确实很爱他们.但是每每想动手写点关于他们的东西,却总是求全心理作祟,始终动不了手. 今天终于下了决心,写点东西,以后可以再修修补补也无妨. 一.为何需要多进程(或者 ...
- Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案
#Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案 ##问题描述 使用Python2.7.x运行GAE时有时会报这个错误 ```py ...
- [笔记]学习HighCharts的使用(不错的web图表插件)
最近有一个小项目需要用到折线图.到处请教了一下,有人给我推荐了highcharts.感觉还不错,就稍微学习下.这里记录一下学习的过程. 网上相关的内容还不少,我就说一下我学习的内容. 看的第一篇文章& ...
- 53.转:深入浅出FPGA-14-ChipScope软件使用
引言 索性再破例一下,成个系列也行. 内容组织 1.建立工程 2.插入及配置核 2.1运行Synthesize 2.2新建cdc文件 2.3 ILA核的配置 3. Implement and gene ...
- < java.util >-- Iterator接口
每一个集合都有自己的数据结构,都有特定的取出自己内部元素的方式.为了便于操作所有的容器,取出元素.将容器内部的取出方式按照一个统一的规则向外提供,这个规则就是Iterator接口. 也就说,只要通过该 ...
- Delphi 调试日子 - TLogger
这段时间又开始用delphi了,才发现我对它这么的不熟悉! 简单的而有效的调试工具 Logger 这个是“榕树下”的作品,小巧而精悍.稍微调整了一下.在需要的地方加入 {$IFDEF DEBUG} ...
- 升级ubuntu内核
ubuntu12.04内核为 linux-image-3.5.0-23-generic 要升级为 linux-image-3.2.0-57-generic 使用apt-get install linu ...
- HashMap优雅的初始化方式以及引申
小记 相信很多人和笔者一样,经常会做一些数组的初始化工作,也肯定会经常用到集合类.假如我现在要初始化一个String类型的数组,可以很方便的使用如下代码: String [] strs = {&quo ...