House Robber I && II

I

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

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.

public class Solution {
    public int rob(int[] num) {
 // https://leetcode.com/discuss/30020/java-o-n-solution-space-o-1 不是很理解
    int prevNo = 0;
    int prevYes = 0;
    for (int n : num) {
        int temp = prevNo;
        prevNo = Math.max(prevNo, prevYes); // here prevNo is the next loop‘s prevNo
        prevYes = n + temp;                 // next loop‘s prevYes
    }
    return Math.max(prevNo, prevYes);

    }
}

II

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.

思路摘抄  http://blog.csdn.net/xudli/article/details/45886721

House Robber I的升级版. 因为第一个element 和最后一个element不能同时出现. 则分两次call House Robber I. case 1: 不包括最后一个element. case 2: 不包括第一个element.

两者的最大值即为全局最大值

public class Solution {
    public int rob(int[] nums) {
        if(nums==null|| nums.length==0) return 0;
        if(nums.length<3) return Math.max(nums[0],nums[nums.length-1]);
        int pn=0, py= 0, r1=0, r2=0;
        for(int i=0;i<nums.length-1;i++){
            int tmp = pn;
            pn = Math.max(pn,py);
            py=nums[i]+tmp;
        }
        r1 = Math.max(pn,py);
        pn=0;
        py=0;
        for(int i=1;i<nums.length;i++){
            int tmp = pn;
            pn = Math.max(pn,py);
            py=nums[i]+tmp;
        }
        r2 = Math.max(pn,py);
        return Math.max(r1,r2);
    }
}
时间: 2024-08-04 15:24:32

House Robber I && II的相关文章

House Robber I &amp; II &amp; III

House Robber You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and 

【LeetCode】House Robber I &amp; II 解题报告

[题目] I You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it wil

解题思路:house robber i &amp;&amp; ii &amp;&amp; iii

这系列题的背景:有个小偷要偷钱,每个屋内都有一定数额的钱,小偷要发家致富在北京买房的话势必要把所有屋子的钱都偷了,但是屋子之内装了警报器,在一定条件下会触发朝阳群众的电话,所以小偷必须聪明一点,才能保证偷到的钱最多. 问题i:这些屋子排成一排,连续两家失窃就会触发朝阳群众电话 问题ii:这些屋子两成一个圈(四合院既视感),连续两家失窃就会触发朝阳群众电话 问题iii:这些屋子组成一棵树,有连接的两个节点同时失窃就会触发朝阳群众电话 i: 思路:一道典型的dp问题,dp[i]表示在小偷路过第i间房

leetCode 213. House Robber II | Medium | Dynamic Programming

213. 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 a

【LeetCode】213. House Robber II

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 arrang

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

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 o

LeetCode之“动态规划”:House Robber &amp;&amp; House Robber II

House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjac

leetcode 213. House Robber II 抢劫房子II---------- java

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. 

213. 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 circ