House Robber && House Robberⅱ

Source: Internet
Author: User

House
robber && House Robberⅱ

house robber
 is a professional robber planning to rob houses along a street. Each house have a certain amount of money stashed, the only constraint stopping all from robbing each of the them are that Adjac ENT houses have security system connected and it would automatically contact the police if  adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.  

A thief steals something from a series of houses (as an array) and cannot steal a neighboring house (an alarm is triggered by the theft of a neighboring house), and the maximum amount of money it can steal. Conversion: From a given array, take the sum of the number, the adjacent can not be taken, the maximum and what is to be taken

思路,定义一个数组dp,dp[i]表示从1到i个数的能偷的最大值,对于i+1,我们知道,前i+1个房子(从1开始)最大值为前i个房子dp值和偷第i+1个房子加上dp[i-1](需要绕开i个房子)两者的最大值,dp[i+1]=Max(dp[i],dp[i-1+nums[i]]),dp[0]=0,dp[1]=nums[1],
 class solution { Public:intRob (vector<int>& nums) {if(Nums.empty ())return 0;intLen=nums.size ();if(len==1)returnnums[0]; vector<int> dp (len+1,0); dp[1]=nums[0]; for(intI=2; i<=len;i++) {Dp[i]=max (dp[i-1],nums[i-1]+dp[i-2]); }returnDp[len]; }intMax (intAintb) {returna>b?a:b; }};
House
Robbereⅱ

Now the owner of the house is smart enough to keep all the houses in a circle, that is, the first house and the last house are adjacent, and ask the thief what the maximum is.

Idea: Since the last and the first house can not be stolen at the same time, then first remove the Last house, ask 1 to len-1 maximum, and then remove the first house, 2 to Len the maximum value of the house, go to the maximum of the two

The code is as follows

 class solution { Public:intRob (vector<int>& nums) {if(Nums.empty ())return 0;intLen=nums.size ();if(len==1)returnnums[0];returnMax (Rob (Nums,0, len-1), Rob (Nums,1, Len)); }intRob (vector<int> &nums,intStartintEnd) {vector<int> dp (end+1,0); dp[start+1]=nums[start]; dp[start]=0; for(inti=start+2; i<=end;i++) {Dp[i]=max (dp[i-1],dp[i-2]+nums[i-1]); }returnDp[end]; }intMax (intAintb) {returna>b?a:b; }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

House Robber && House Robberⅱ

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.