[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] House Robber II 求循环数组中元素两两不相邻的子序列最大和

声明:原题目转载自LeetCode,解答部分为原创 Problem :     Note: This is an extension of House Robber. [LeetCode] ...
  • xblog_
  • xblog_
  • 2017年06月12日 16:28
  • 97

leetcode_337. House Robber III 大盗抢劫,房子是二叉树形式,深度优先遍历

题目: The thief has found himself a new place for his thievery again. There is only one entrance ...

<LeetCode OJ> (198 / 213) House Robber(I / II)

House Robber My Submissions Question Total Accepted: 45702 Total Submissions: 142460 Difficulty:...

LeetCode-难题集之House_Robber系列

记——House_Robber,这个系列的题的思路也很简单,不过大神的代码就是给力,因此记下留着以后学习参考。 House_Robber class Solution { public: ...
  • ww2041
  • ww2041
  • 2016年07月26日 10:25
  • 97

LeetCode 337. House Robber III

大概题意:给定一棵二叉树,每个节点都有一个值,要求从其中选取若干个节点,是的值的和最大,约束条件为任意两个选取的节点之间不能相连。           解题的思路也很清晰,我们可以用动态规划的做法,...
  • isNoel
  • isNoel
  • 2016年12月22日 18:49
  • 169

leetcode 337:House Robber III

leetcode 337:House Robber III。leetcode新题,递归的动态规划

LeetCode 198 House Robber

假设是一个小偷(这比喻,,,),在这条街上有一排房子,每一个房子都有一个确定的价值,相邻的房子不能连续偷窃,问,偷了这条街后,最大偷窃的价值总和是多少? 利用DP思想,对于从第4个房子开始,有两种选择...

LeetCode House Robber题解

House_Robber 题目如下所示 You are a professional robber planning to rob houses along a street. Each house...
  • qisong3
  • qisong3
  • 2015年04月10日 10:38
  • 287

leetcode_middle_31_337. House Robber III

分析: 找出二叉树中最大的不相邻结点的值的和 题意: 一开始的想法是用一个布尔型参数确定下一个结点的值取不取,想的是就两种情况,一层取,下一层就不去...: private void h...
  • pusude
  • pusude
  • 2017年02月08日 19:39
  • 106

LeetCode - 337. House Robber III

这道题目具有Binary Tree的结构并且拥有最优子结构这一性质,也就是说如果我们想从当前的root抢劫到最多的钱,那么我们也希望在root的left和right子树上面抢到最多的钱。题目中已经给出...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Leetcode]House Robber2
举报原因:
原因补充:

(最多只允许输入30个字)