zoukankan      html  css  js  c++  java
  • *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 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.

    思路:

    This is an extension of House Robber. There are two cases here 1) 1st element is included and last is not included 2) 1st is not included and last is included. Therefore, we can use the similar dynamic programming approach to scan the array twice and get the larger value.

    代码:

     1 public int rob(int[] nums) {
     2     if(nums==null||nums.length==0)
     3         return 0;
     4  
     5     int n = nums.length;
     6  
     7     if(n==1){
     8         return nums[0];
     9     }    
    10     if(n==2){
    11         return Math.max(nums[1], nums[0]);
    12     }
    13  
    14     //include 1st element, and not last element
    15     int[] dp = new int[n+1];
    16     dp[0]=0;
    17     dp[1]=nums[0];
    18  
    19     for(int i=2; i<n; i++){
    20         dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i-1]);
    21     }
    22  
    23     //not include frist element, and include last element
    24     int[] dr = new int[n+1];
    25     dr[0]=0;
    26     dr[1]=nums[1];
    27  
    28     for(int i=2; i<n; i++){
    29         dr[i] = Math.max(dr[i-1], dr[i-2]+nums[i]);
    30     }
    31  
    32     return Math.max(dp[n-1], dr[n-1]);
    33 }

    运行结果:

    输入nums={1,4,5,2,9,8}

    1. 包括1st,不包括last one

    dp[0]=0

    dp[1]=nums[0]=1

    dp[2]=4,   (dp[1], dp[0]+nums[1])

    dp[3]=6,   (dp[2], dp[1]+nums[2])

    dp[4]=6,   (dp[3], dp[2]+nums[3])

    dp[5]=15, (dp[4], dp[3]+nums[4])

    2. 不包括1st,包括last one

    dr[0]=0

    dr[1]=4

    dr[2]=5,   (dr[1], dr[0]+nums[2])

    dr[3]=6,   (dr[2], dr[1]+nums[3])

    dr[4]=14, (dr[3], dr[2]+nums[4])

    dr[5]=14, (dr[4], dr[3]+nums[5])

    Reference: http://www.programcreek.com/2014/05/leetcode-house-robber-ii-java/

    解法二:

    public class Solution {
     public int rob(int[] nums) {
        if(nums==null||nums.length==0)
            return 0;
     
        int n = nums.length;
     
        if(n==1){
            return nums[0];
        }    
        if(n==2){
            return Math.max(nums[1], nums[0]);
        }
     
        //include 1st element, and not last element
        int[] dp = new int[n+1];
        dp[0]=nums[0];
        dp[1]=nums[0]; //include 1st element, so doesnt consider the 2nd element
     
        for(int i=2; i<n; i++){
            dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i]);  
        }
     
        //not include frist element, and include last element
        int[] dr = new int[n+1];
        dr[0]=0;
        dr[1]=nums[1];
     
        for(int i=2; i<n; i++){
            dr[i] = Math.max(dr[i-1], dr[i-2]+nums[i]);
        }
        return Math.max(dp[n-2], dr[n-1]);  //dp[n-1] include the last element, dp[n-2] is correct. 
    }
    }
  • 相关阅读:
    蓝桥杯历届试题 打印十字图 文字图形
    Cuckoo Hashing
    2006 飞行员配对(二分图最大匹配)
    Bad Hair Day(求数组中元素和它后面离它最近元素之间的元素个数)
    2019CCPC江西省赛
    字典树系统学习
    ac自动机学习
    项目管理(把与某点相邻边分为两类 是复杂度降为(n^(3/2))
    Ultra-QuickSort(离散化)
    Chika and Friendly Pairs(莫队+树状数组+离散化+预处理上下界)
  • 原文地址:https://www.cnblogs.com/hygeia/p/4647856.html
Copyright © 2011-2022 走看看