A simple cpp solution


  • 6
    J
    class Solution {
    public:
        int maxSubArray(int A[], int n) {
            // Note: The Solution object is instantiated only once and is reused by each test case.
            int maxSum = -9999999, curSum = -9999999;
            for (int i=0; i<n; ++i) {
                curSum = max(curSum + A[i], A[i]);
                maxSum = max(maxSum, curSum);
            }
            return maxSum;
        }
    };

  • 7
    A

    I think this solution will be better if maxSum and curSum are initialized to A[0], and the index i starts from 1


  • 0
    E

    You can use curSum = -INT_MAX instead of -9999999


  • 0
    S

    Can we use INT_MIN rather -INT_MAX?


Log in to reply
 

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