Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Maximum Subarray


Solution


    class Solution {
    public:
        int maxSubArray(vector<int>& nums) {
            int maxSum = INT_MIN;
            int currSum = 0;
            for(int i = 0; i < nums.size(); i++) {
                currSum += nums[i];
                if(currSum > maxSum)
                    maxSum = currSum;
                if(currSum < 0)
                    currSum = 0;
            }
            return maxSum;
        }
    };