clueless coding // TODO: be smarter

LeetCode Maximum Subarray: CPP Solution



TL;DR Code Solution


int maxSubArray(vector<int>& nums) {

    int current = 0;
    int result = nums[0];
    for (int i = 0; i < nums.size(); i++) {
        current += nums[i];
        result = max(current, result);
        if (current < 0) {
            current = 0;
        }
    }
    return result;

}