Leetcode 53 Solution

This article provides solution to leetcode question 53 (maximum-subarray)

https://leetcode.com/problems/maximum-subarray

Solution

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        vector<int> b;
        b.resize(nums.size());

        int res = INT_MIN;

        for (int i = 0; i < nums.size(); i++)
        {
            if (i == 0)
                b[i] = nums[i];
            else
                b[i] = max(b[i - 1], 0) + nums[i];

            res = max(res, b[i]);
        }

        return res;
    }
};