Skip to content

Latest commit

 

History

History
25 lines (24 loc) · 636 Bytes

209. Minimum Size Subarray Sum.md

File metadata and controls

25 lines (24 loc) · 636 Bytes

209. Minimum Size Subarray Sum (Medium)

[tag] two pointers, sliding window

  • no negetive integers so can use two pointers/sliding window.
  • can we use hashmap???
'''
  l
2,3,1,2,4,3
        r
'''
class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        ans = len(nums) + 1
        window_sum = 0
        l = r = 0
        while r < len(nums):
            window_sum += nums[r]
            while window_sum >= target:
                ans = min(ans, r - l + 1)
                window_sum -= nums[l]
                l += 1
            r += 1
        return 0 if ans > len(nums) else ans