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的更多相关文章

  1. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  2. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  3. [LeetCode]House Robber II (二次dp)

    213. House Robber II     Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...

  4. LeetCode之“动态规划”:House Robber && House Robber II

    House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...

  5. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  6. 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:二叉树下的不能相邻,求能 ...

  7. 【刷题-LeetCode】213. House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

  8. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  9. Leetcode House Robber II

    本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...

  10. 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 ...

随机推荐

  1. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统 :1.技术简介之Mina连接

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Apache MINA(Multipurpose Infrastructure for Network Applic ...

  2. c++性能测试

    程序分析是以某种语言书写的程序为对象,对其内部的运作流程进行分析.程序分析的目的主要有三点:一是通过程序内部各个模块之间的调用关系,整体上把握程序的运行流程,从而更好地理解程序,从中汲取有价值的内容. ...

  3. 初始twisted(一)

    1.与同步模型的优势: 1.有大量的任务,一个时刻内至少有一个任务要运行 2.任务执行大量的I/O,同步模型会因为任务阻塞而浪费大量时间 3.任务之间相互独立,任务内部交互少. 2.与同步模式客户端的 ...

  4. 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较

    2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...

  5. WPF窗口长时间无人操作鼠标自动隐藏

    在软件开发中有时会有等待一段时间无人操作后隐藏鼠标,可能原因大致如下: 1.为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面: 2.软件为了 ...

  6. bootstrap bootstrapTable 隐藏列

    主要代码: <script type="text/javascript"> $(function () { LoadingDataListOrderRealItems( ...

  7. 数组和字典 swift

    var array = ["A","B"] var array2:[String] = ["A","B"] var ar ...

  8. LinuxC 文件与目录 打印文件操作错误信息

    打印文件操作错误信息 在进行文件操作是,会遇到权限不足.找不到文件等错误,可以在程序中设置错误捕捉语句并显示错误.错误捕捉和错误输出使用用错误号和streero实现. 函数原型 : char *str ...

  9. linux查看tomcat版本

    进入tomcat bin目录下 然后执行 ./version.sh Server version: Apache Tomcat/6.0.26Server built:   March 9 2010 1 ...

  10. MVC 基础知识

    一. MVC架构1.MVC模式是一种严格实现应用程序各部分隔离的架构模式.隔离:分离关注点,松耦合2.模型(Model) 代表着核心的业务逻辑和数据.模型封装了域实体的属性和行为3.视图(View) ...