[Leetcode]House Robber2

转载 2015年07月08日 09:15:07


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 arearranged 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 tonightwithout alerting the police.


Since this question is a follow-up to House Robber, we can assume we already have a way to solve the simpler question, i.e. given a 1 row of house, we know how to rob them. So we already have such a helper function. We modify it a bit to rob a given range of houses.

Now the question is how to rob a circular row of houses. It is a bit complicated to solve like the simpler question. It is because in the simpler question whether to robnum[lo] is entirely our choice. But, it is now constrained by whether num[hi] is robbed.

However, since we already have a nice solution to the simpler problem. We do not want to throw it away. Then, it becomes how can we reduce this problem to the simpler one. Actually, extending from the logic that if house i is not robbed, then you are free to choose whether to rob house i + 1, you can break the circle by assuming a house is not robbed.

For example, 1 -> 2 -> 3 -> 1 becomes 2 -> 3 if 1 is not robbed.

Since every house is either robbed or not robbed and at least half of the houses are not robbed, the solution is simply the larger of two cases with consecutive houses, i.e. house i not robbed, break the circle, solve it, or house i + 1 not robbed. Hence, the following solution. I chose i = n and i + 1 = 0 for simpler coding. But, you can choose whichever two consecutive ones.


private int rob(int[] num, int lo, int hi) {
    int include = 0, exclude = 0;
    for (int j = lo; j <= hi; j++) {
        int i = include, e = exclude;
        include = e + num[j];
        exclude = Math.max(e, i);
    return Math.max(include, exclude);

public int rob(int[] nums) {
    if (nums.length == 1) return nums[0];
    return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));

[leetcode 256] Paint House

Question: There are a row of n houses, each house can be painted with one of the three colors: red,...
  • Xiaohei00000
  • Xiaohei00000
  • 2016年01月13日 21:36
  • 827

leetCode198-House Robber

链接:https://leetcode.com/problems/house-robber/ 这道理可以看做是状态压缩,每两个数字看做是一行,状态有3个,故需要F[N][3]的数组,F[i][j]就表...
  • Lu597203933
  • Lu597203933
  • 2015年04月01日 18:28
  • 7407

Leetcode_198_House Robber

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47680663 You are a profes...
  • pistolove
  • pistolove
  • 2015年08月15日 12:02
  • 2116

leetcode 213 : House Robber II

leetcode 213 : House Robber II
  • xudli
  • xudli
  • 2015年05月21日 07:36
  • 9550

【LeetCode-面试算法经典-Java实现】【198-House Robber(抢劫犯)】

【189-House Robber(抢劫犯)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao】原题  Y...
  • 2015年08月25日 06:41
  • 3807

[leetcode] 256. Paint House 解题报告

题目链接:https://leetcode.com/problems/paint-house/ There are a row of n houses, each house can be pai...
  • qq508618087
  • qq508618087
  • 2016年03月13日 03:03
  • 1458

[leetcode-337]House Robber III(java)

原题:House RobberIII对于这种树形结构,应该很容易想到使用递归的方法,这里的难点在于递归的时候相邻点不能同时访问,因此我写成了第一种做法,也ac了,但是这样并不好,因为它对很多点都会重复...
  • zdavb
  • zdavb
  • 2016年03月27日 22:19
  • 831

Leetcode 213 House Robber II 抢劫最大金额

  • smile_watermelon
  • smile_watermelon
  • 2015年08月19日 23:38
  • 561

[leetcode] 265. Paint House II 解题报告

题目链接:https://leetcode.com/problems/paint-house-ii/ here are a row of n houses, each house can b...
  • qq508618087
  • qq508618087
  • 2016年03月05日 12:50
  • 1436

LeetCode337. House Robber III

题目: https://leetcode.com/problems/house-robber-iii/ The thief has found himself a new place for hi...
  • codeTZ
  • codeTZ
  • 2016年03月23日 20:34
  • 1100
您举报文章:[Leetcode]House Robber2