Very concise O(n) dp solution


  • 0
    S
    int maxSubArray(int A[], int n) {
        int max = A[0], sum = A[0];
        for (int i = 1; i < n; ++i) {
            sum += A[i];
            if (A[i] > sum) sum = A[i];
            if (sum > max) max = sum;
        }
        
        return max;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.