House Robber && House Robberⅱ__leetcode

Source: Internet
Author: User
House robber && House Robberⅱ

House robber

You are are a professional robber planning to rob houses along a street. Each house has a certain amount's stashed, the only constraint stopping to robbing each of this them is this Adjac ENT houses have security system connected and it'll 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 all house, determine the maximum amount of mone Y you can rob tonight without alerting the police.

A rogue steals from a series of houses (which can be seen as an array) and cannot steal from neighboring houses (the adjacent houses will trigger an alarm) and how much can they steal. Conversion: From a given array, the sum of the numbers, the adjacent cannot be taken, the maximum and how much can be taken

The idea of defining an array dp,dp[i] represents the maximum number of steals from 1 to I, for i+1, we know that the first i+1 house (starting from 1) has the maximum value for the former I house DP and steals the I+1 House plus dp[i-1] (need to bypass I house) the maximum value of both, dp[i+1 ]=max (Dp[i],dp[i-1+nums[i]]), dp[0]=0,dp[1]=nums[1],
Class Solution {public
:
    int Rob (vector<int>& nums) {
        if (Nums.empty ()) return
            0;
        int len=nums.size ();
        if (len==1) return
            nums[0];

        Vector<int> DP (len+1,0);

        Dp[1]=nums[0];
        for (int i=2;i<=len;i++)
        {
            Dp[i]=max (dp[i-1],nums[i-1]+dp[i-2]);
        }

        return Dp[len];

    }

    int Max (int a,int b)
    {return
        a>b?a:b;
    }
};
House Robbereⅱ

Now the owner of the house is smart, and all the houses in a circle, that is, the first house and the last house is adjacent, ask the thieves can steal the maximum value is how much

Thought: Since the last one and the first house can not be stolen at the same time, then remove the last House, 1 to len-1 maximum, and then remove the first house, 2 to Len the maximum value of the house, go to the maximum value of both

The code is as follows

Class Solution {public
:
    int Rob (vector<int>& nums) {

        if (Nums.empty ()) return
            0;
        int len=nums.size ();
        if (len==1) return
            nums[0];

        Return Max (Rob (nums,0,len-1), Rob (Nums,1,len));

    int Rob (vector<int> &nums,int Start,int end)
    {
        vector<int> dp (end+1,0);

        Dp[start+1]=nums[start];
        dp[start]=0;

        for (int i=start+2;i<=end;i++)
        {
            Dp[i]=max (dp[i-1],dp[i-2]+nums[i-1]);
        }

        return dp[end];
    }

    int Max (int a,int b)
    {return
        a>b?a:b;
    }
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.