Question: https://leetcode.com/problems/minimum-size-subarray-sum/

Question Name: Minimum Size Subarray Sum

This question is very similar with Longest Substring Without Repeating Characters and Minimum Window Substring.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Solution(object): def minSubArrayLen(self, s, nums): """ :type s: int :type nums: List[int] :rtype: int """ begin = 0 # Inclusive end = 0 # Exclusive. It is always different from "begin" # after the first move. window_sum = 0 result = 0 # Find candidate windows, whose sum is >= s. while end < len(nums): # Prepare for this window. window_sum += nums[end] end += 1 # Extend the window by moving the "end". # For the first window, it may be extended by multiple steps. # Otherwise, it is always extended by one step. while end < len(nums) and window_sum < s: window_sum += nums[end] end += 1 if window_sum < s: break # Shrink the window by moving the "begin". while window_sum - nums[begin] >= s: window_sum -= nums[begin] begin += 1 # Update the result if this window is shorter. if result == 0 or end - begin < result: result = end - begin return result |