213 House Robber II

Source: Internet
Author: User

    /** 213.      House Robber II * 12.30 by Mingyang * Because can't rob two, so I blindly to find the first to be robbed or not, but the fact is divided two times call house robber I. * Case 1: The last element is not included. Case 2: The first element is not included. The maximum value of both is the global maximum * just beginning to enter a misunderstanding, the first do not, the last must be, is not right, because the first do not, the last one does not have to be * the same, attention is the first second need special judgment, respectively, they compare Who's big who's small, followed by DP in turn*/     Public intROB2 (int[] nums) {        if(nums==NULL|| nums.length==0)return0; if(nums.length==1)returnNums[0]; if(nums.length==2)returnMath.max (Nums[0], nums[1]); returnMath.max (Rob (nums, 0, Nums.length-2), Rob (Nums, 1, nums.length-1)); }      Private intRobint[] Nums,intSinte) {intn = e-s + 1; int[] DP =New int[n]; dp[0] =Nums[s]; dp[1] = Math.max (Nums[s], nums[s+1]);  for(intK=p; i<n; i++) {Dp[i]= Math.max (Dp[i-2]+nums[s+i], dp[i-1]); }        returnDp[n-1]; }    

213 House Robber II

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.