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两次,再求最大值。

  1. public class Solution {
  2. public int rob(int[] nums) {
  3. if(nums.length == 0) {
  4. return 0;
  5. }
  6. if(nums.length == 1) {
  7. return nums[0];
  8. }
  9. int prepre = 0;
  10. int pre = 0;
  11. int result = pre;
  12. int max1 = 0;
  13. for(int i = 0; i < nums.length - 1; i++) {
  14. result = Math.max(pre, prepre + nums[i]);
  15. prepre = pre;
  16. pre = result;
  17. }
  18. max1 = result;
  19.  
  20. int max2 = 0;
  21. prepre = 0;
  22. pre = 0;
  23. result = pre;
  24.  
  25. for(int i = 1; i < nums.length; i++) {
  26. result = Math.max(pre, prepre + nums[i]);
  27. prepre = pre;
  28. pre = result;
  29. }
  30. max2 = result;
  31. return Math.max(max1, max2);
  32. }
  33. }

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. UITextField使用的相关方法

    1.设置占位符 textField.placeholder = @“”; 2.设置暗文输入 textField.secureTextEntry = YES;   3.设置键盘类型 textField. ...

  2. bootstrap插件之Carousel

    兼容:ie9以上 特点:滑动图片看起来永远只有两帧,过度完美:是html css js的完美配合:其中html的data属性起了关键性作用 前提:normalize.css  jquery.js ht ...

  3. java intellij 写控制台程序 窗口程序

    建一个空项目,建一个main函数  用application,就可以运行了 /**  * Created by robin on 15/10/11. */public class hello { pu ...

  4. TFS(Taobao File System)安装方法

    文章目录: 一.TFS(Taobao File System)安装方法 二.TFS(Taobao File System)配置dataServer.分区.挂载数据盘 三.TFS(Taobao File ...

  5. Object-c 语法 - 头文件引用(@class/#import/#include)

    一. Objective-C 中 #import 和 #include 的区别 预编译指令 Objective-C:#import:由gcc编译器支持 C,C++:#include 在 Objecti ...

  6. 20.时钟抖动(jitter)和时钟偏移(skew)的概念?

    jitter:由于晶振本身稳定性,电源以及温度变化等原因造成了时钟频率的变化,就是jitter,指的是时钟周期的变化.指两个时钟周期之间存在的差值,这个误差是在时钟发生器内部产生的,和晶振或者PLL内 ...

  7. Java Day 13

    线程的状态 被创建 运行 冻结 消亡  被创建--start()--> 运行 运行----run()----> 消亡         stop() 运行---sleep(time)---& ...

  8. COS中访问文件的三种方式

    1.通过FID来访问文件(比如EF,DF) 2.通过SFI来访问文件(有些COS命令可以通过SFI来快速访问文件,而不需要事先选中文件) 3.通过文件名来访问文件(只能是DF文件)

  9. The income statement

    The income statement measures performance over some period  of time,usually a quarter or a year.The ...

  10. Netsharp FAQ

    1.启动服务器时候不能监听端口,出现如下错误: 回答: 这种问题应该是在win7环境下才有,是没有权限,要以管理员身份运行Netsharp.Elephant.Q.exe. 2.启动服务器的时候,服务端 ...