JavaScript (greedy)


  • 0
    I
    var maxSubArray = function(nums) {
        if(!nums || nums.length === 0){
            return 0
        }
    
        let max = -Number.MAX_VALUE;
        let sum = 0
        for( let i = 0 ; i < nums.length ; i++){
            sum += nums[i]
            max = Math.max(max, sum)
            sum = Math.max(sum, 0)
        }
        
        return max
    };
    
    

Log in to reply
 

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