Posted on

Description

Submission

If we find the currSum is smaller than 0, we should restart the summing.

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int currSum = 0, sum = nums[0];
        for(int num: nums) {
            
            if(currSum < 0) currSum = 0;
            currSum += num;
            if(currSum > sum) sum = currSum;
        }
        return sum;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *