53.Maximum Subarray

Source: Internet
Author: User

    /** 53.Maximum Subarray * 2016-5-7 by Mingyang * If we traverse this array from the beginning.     For one of the elements in the array, it has only two choices: 1. * Either join the previous array Plus and in (with another group) * 2. Or you can set up an array of your own (one by one) * So how to choose this element depends on what group he will contribute to.     If you have a group with others * can make the total increase and get bigger, or with others a group of good, if you start a group, the value of their own than the previous sums value is still larger, then you open a single set of good. * So use a DP array to record the maximum value for each round of sum, Dp[i] Indicates whether the current element is preceded by an array plus a set or a single set of itself, and then maintains a global maximum of the ascended answer * then this topic starts to think can be used Maxsubarray (int a[], int i , Int j), which means the Maxsubarray for a[i:j]. * But it's hard to find this kind of relationship by writing a sub-function like this.     So what do we do next?     * Maxsubarray (int a[], int i), which means the Maxsubarray for A[0:I] which must have a[i] as the end element.     * Then the relationship is: * Dp[i] = Math.max (A[i], dp[i-1] + a[i]);     * That is to say, for A[i] in the end, we just need to see this add in big or alone big.     * Because if you add in is bigger than the individual, then the back is an increment.     * Never write: dp[i] = Math.max (Dp[i-1], dp[i-1] + a[i]); */     Public Static intMaxsubarray (int[] A) {int[] DP =New int[A.length]; intmax = A[0]; dp[0] = a[0];  for(inti = 1; i < a.length; i++) {Dp[i]= Math.max (A[i], dp[i-1] + a[i]);//here only compare their own open another, and the original plus open the differenceMax =Math.max (Max, dp[i]); }        returnMax; }    /** Below is my own solution, began to think a bit complex, and then a careful enumeration is quite simple, here with a DP array * Dp[i] to represent the maximum of continuous array including I, rather than to I optimal results * [ -1,-2] so dp[1]=-3, because To include the 2. * Then if each number itself is very large, than the previous cumulative DP is larger, so long playing independently * otherwise need to add up, each time the DP is updated with the global variable Max compared*/     Public intMaxSubArray1 (int[] nums) {        intlen=nums.length; intMax=nums[0]; int[] dp=New int[Len]; dp[0]=nums[0];  for(inti=1;i<len;i++){            if(nums[i]>nums[i]+dp[i-1]) {Dp[i]=Nums[i]; Max=Math.max (Max,dp[i]); }Else{Dp[i]=dp[i-1]+Nums[i]; Max=Math.max (Max,dp[i]); }        }        returnMax; }

53.Maximum Subarray

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.