Javascript O(n) solution


  • 0
    H

    A simple solution (Kadane's algorithm) in Javascript

    /**
     * @param {number[]} nums
     * @return {number}
     */
    
    var maxSubArray = function(nums) {
        let max_so_far = -Infinity;
        let max_here = 0;
        
        for (let i = 0; i < nums.length; i++) {
            const val = nums[i];
            max_here = Math.max(val, max_here + val);
            max_so_far = Math.max(max_here, max_so_far);
        }
        
        return max_so_far;
    };
    

Log in to reply
 

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