dp solutions


  • 0
    W

    suppose the big num end with nums[i] and it will add the largest value in front

    /**
     * @param {number[]} nums
     * @return {number}
     */
    var maxSubArray = function(nums) {
        //DP solutions
        var dp = [];
        dp[0] = nums[0];
        var max = nums[0];
        for(var i = 1;i<nums.length;i++){
            dp[i] = nums[i] + (dp[i-1]>0?dp[i-1]:0);
            max = Math.max(dp[i],max);
        }
        return max;
    };
    

Log in to reply
 

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