孙晓聪
  • 最新
  • 博客
  • 书评
  • Minimum Size Subarray Sum

    Description

    doc

    Solutions

    First Idea

    /**
     * @param {number} target
     * @param {number[]} nums
     * @return {number}
     */
    var minSubArrayLen = function (target, nums) {
      let [start, end] = [0, 0]
      let sum = 0
      let minLen = Number.MAX_SAFE_INTEGER
    
      while (end < nums.length) {
        sum += nums[end++]
    
        while (start < end && sum >= target) {
          minLen = Math.min(minLen, end - start)
          sum -= nums[start++]
        }
      }
    
      return minLen === Number.MAX_SAFE_INTEGER ? 0 : minLen
    }
    
    • Time Complexity: O(n)
    • Space Complexity: O(1)