Leetcode_num15_Maximun Subarray, leetcode

Source: Internet
Author: User

Leetcode_num15_Maximun Subarray, leetcode

Question: Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array[−2,1,−3,4,−1,2,1,−5,4],
The contiguous subarray[4,−1,2,1]Has the largest sum =6.

Click to show more practice.

More practice:

If you have figured out of the O (n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

The requirement of this question is to find the continuous array segment with the largest sum in the given array, and use the o (n) and divide and conquer methods respectively.

The o (n) method is relatively simple, that is, to traverse the array in sequence, record the current sum and maximum value: if the current number is negative, then calculate and add the following sum value, and compare it with the maximum value, if the value is greater than the maximum value, the maximum value is changed. After that, the sum value is returned to zero, starting with the next value as the fragment. If the current number is not negative, you only need to compare the sum and maximum values after adding them. If the sum value is greater than the maximum value, the maximum value is changed. The Code is as follows:

class Solution:    # @param A, a list of integers    # @return an integer    def maxSubArray(self, A):        L=len(A)        rs=0        m=A[0]        for i in range(0,L):            rs+=A[i]            if rs<0:                if rs>m:                    m=rs                rs=0            else:                if rs>m:                    m=rs                    return m

Divide and conquer method, that is, merge. The algorithm complexity is o (nlogn), and the formula is T (n) = 2 * T (n/2) + o (n)

Recursive call is used to determine whether the intermediate vertex is included in the final array fragment by dividing the intermediate vertex of the array fragment. Therefore, we need to compare the size of the three values: contains the maximum value of the intermediate vertex, excluding the maximum value of the intermediate vertex (that is, the maximum value of the Left array fragment and the maximum value of the right array fragment), and returns the result with the largest value selected.

The last two values can be obtained through recursive function calls;

To find the maximum value containing the intermediate vertex, you need to find the upper bound of the left extension and the lower bound of the right extension. Therefore, the center point is used as the start point to sum left. The initial value of the maximum value is set to the value of the center point. If the current value is greater than the maximum value, the maximum value is modified. Likewise, start from the right point of the intermediate point and sum to the right. The initial value of the maximum value is set to the value of this point. If the current value is greater than the maximum value, modify the maximum value and add the two maximum values, you can obtain the maximum and value of the intermediate vertex. The specific code is as follows:

class Solution:    # @param A, a list of integers    # @return an integer    def maxSubArray(self, A):        def maxS(A,left,right):            if left==right: return A[left]            middle=(left+right)/2            leftm=maxS(A,left,middle)             rightm=maxS(A,middle+1,right)            lm=A[middle]            rm=A[middle+1]            t=0            for i in range(middle,left-1,-1):                t+=A[i]                if(t>lm):                    lm=t            t=0            for i in range(middle+1,right+1):                t+=A[i]                if(t>rm):                    rm=t            return max(leftm,rightm,lm+rm)                    L=len(A)        return maxS(A,0,L-1)





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.