Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

maximum subarray leetcode c++

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int max_sum = INT_MIN;
        int sum = 0;

        for(int i = 0; i < nums.size(); i++){
           sum += nums[i];

            if(max_sum < sum){
                max_sum = sum;
            }

            if(sum < 0){
                sum = 0;
            }
        }

        return max_sum;
    }
};
Source by alkeshghorpade.me #
 
PREVIOUS NEXT
Tagged: #maximum #subarray #leetcode
ADD COMMENT
Topic
Name
1+9 =