LC 53. Maximum Subarray

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int n = nums.size();
        int dp[n];
        int sum = nums[0];
        dp[0] = nums[0];
        for(int i=1;i<n;i++){
            dp[i] = max(nums[i],nums[i]+dp[i-1]);
            sum = max(sum,dp[i]);
        }
        return sum;
    }
};


Dicussion Forum