Maximum Subarray

 

CREATED TIME: Jan 12, 2020 12:03 AM UPDATED TIME: Jan 12, 2020 12:03 AM 난이도: Easy

class Solution { public: int maxSubArray(vector& nums) { int size = nums.size();

        vector<int> dp(size, 0);
        dp[0] = nums[0];
        int res = dp[0];
        for(int i=1; i<size; i++){
            if (dp[i - 1] + nums[i] > nums[i]){
                dp[i] = dp[i-1] + nums[i];
            }else{
                dp[i] = nums[i];
            }
            res = max(res, dp[i]);
        }
        return res;
    }
};