algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

minimum-size-subarray-sum.py (829B)


      1 class Solution:
      2     def minSubArrayLen(self, target: int, nums: List[int]) -> int:
      3         
      4         left = 0
      5         right = 0
      6 
      7         current_sum = nums[0]
      8         best_length = 0
      9 
     10         while right < len(nums):
     11             if current_sum >= target:
     12                 current = right - left
     13                 if best_length == 0 or best_length > current + 1:
     14                     best_length = current + 1
     15                 if left < right: 
     16                     current_sum -= nums[left]
     17                     left += 1
     18 
     19                 else:
     20                     right += 1
     21                     if right < len(nums):
     22                         current_sum += nums[right]
     23             else:
     24                 right += 1
     25                 if right < len(nums):
     26                     current_sum += nums[right]
     27 
     28         
     29         return best_length