孙晓聪
  • 最新
  • 博客
  • 书评
  • Maximum Subarray

    Description

    doc

    Solutions

    Dynamic Programing

    /**
     * @param {number[]} nums
     * @return {number}
     */
    var maxSubArray = function (nums) {
      let sum = (max = nums[0])
    
      for (let i = 1; i < nums.length; i++) {
        const sumWithCurrent = sum + nums[i]
    
        sum = sumWithCurrent < nums[i] ? nums[i] : sumWithCurrent
        max = sum > max ? sum : max
      }
    
      return max
    }