Min-size subarray sum
The next few questions for this week will all be sliding window questions.
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
pointer_1 = 0
pointer_2 = 0
curr_sum = nums[pointer_1]
num_length = len(nums)
min_subarray = num_length + 1
while pointer_2 < num_length:
# check
if curr_sum >= target:
curr_length = pointer_2 - pointer_1 + 1
if curr_length < min_subarray:
min_subarray = curr_length
# move
if pointer_1 == pointer_2:
pointer_2 = pointer_2 + 1
if pointer_2 >= num_length:
break
curr_sum = curr_sum + nums[pointer_2]
elif curr_sum >= target:
curr_sum = curr_sum - nums[pointer_1]
pointer_1 = pointer_1 + 1
elif curr_sum < target:
pointer_2 = pointer_2 + 1
if pointer_2 >= num_length:
break
curr_sum = curr_sum + nums[pointer_2]
if min_subarray == num_length + 1:
return 0
return min_subarray