Welcome to LeetCode Discuss.  Please read the FAQ to help yourself making the best use of Discuss.
Ask a Question
Back to Problem

Welcome to LeetCode Discuss.

This is a place to ask questions related to only OJ problems.

Please read the FAQ to help yourself making the best use of Discuss.

how to solve "Maximum Subarray" by using the divide and conquer approach ?

+4 votes
591 views

I solve this problem in O(n). But the hint says it would be solved by using the divide and conquer approach. I cannot figure out how to do it with divide and conquer. You guys have ideas?

asked Nov 21, 2013 in Maximum Subarray by chentao169 (1,800 points)

What's the algorithm for O(n)..? I can only come up with an O(n^2) one.. and the one below (in the answer) is O(n log n)

For divide and conquer approach, I think O(N log N) is the best time complexity.

Here is the o(n) solution : http://stackoverflow.com/questions/16605991/number-of-subarrays-divisible-by-k

2 Answers

+15 votes
 
Best answer

Step1. Select the middle element of the array. So the maximum subarray may contain that middle element or not.

Step 2.1 If the maximum subarray does not contain the middle element, then we can apply the same algorithm to the the subarray to the left of the middle element and the subarray to the right of the middle element.

Step 2.2 If the maximum subarray does contain the middle element, then the result will be simply the maximum suffix subarray of the left subarray plus the maximum prefix subarray of the right subarray

Step 3 return the maximum of those three answer.

Here is a sample code for divide and conquer solution. Please try to understand the algorithm before look at the code

class Solution {
public:
    int maxSubArray(int A[], int n) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(n==0) return 0;
        return maxSubArrayHelperFunction(A,0,n-1);
    }

    int maxSubArrayHelperFunction(int A[], int left, int right) {
        if(right == left) return A[left];
        int middle = (left+right)/2;
        int leftans = maxSubArrayHelperFunction(A, left, middle);
        int rightans = maxSubArrayHelperFunction(A, middle+1, right);
        int leftmax = A[middle];
        int rightmax = A[middle+1];
        int temp = 0;
        for(int i=middle;i>=left;i--) {
            temp += A[i];
            if(temp > leftmax) leftmax = temp;
        }
        temp = 0;
        for(int i=middle+1;i<=right;i++) {
            temp += A[i];
            if(temp > rightmax) rightmax = temp;
        }
        return max(max(leftans, rightans),leftmax+rightmax);
    }
};
answered Nov 21, 2013 by porker2008 (6,890 points)
selected Nov 21, 2013 by chentao169

Great. what is the time complexity?

O(N log N) is the running time

Neat algorithm!

T(n) = 2 * T(n/2) + n You can check the Master Theorem to gain time complexity.

very good and neat alogirthm

0 votes

Here is my answer, not sure if it is a divide and conquer approach. Maybe it is just the normal answer.

class Solution {
 public: int maxSubArray(int A[], int n) { 
     int s[n]; 
     int max = s[0] = A[0]; 
     for (int i = 1; i < n; i++) {
        s[i] = s[i-1] > 0 ? (A[i] + s[i-1]) : A[i]; 
        max = std::max(max, s[i]); 
     }
     return max;
 }
};
answered Feb 1 by raywill (290 points)

This is not divide and conquer, but the O(n) solution. Very elegant indeed.


...