Kadane's algo 5 line algo


  • 16
    K

    class Solution
    {
    public:

    int maxSubArray(vector<int>& nums) {
        int sum=nums[0],lgsum=nums[0];
        for(int i=1;i<nums.size();i++)
        {
            sum=max(nums[i],sum+nums[i]);
            lgsum=max(lgsum,sum);
        }
        return lgsum;
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.