-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0209_Minimum_Size_Subarray_Sum.py
39 lines (34 loc) · 1.14 KB
/
0209_Minimum_Size_Subarray_Sum.py
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
33
34
35
36
37
38
39
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
sum_nums = sum(nums)
len_nums = len(nums)
sol = len_nums
# Special Case
if sum_nums < target:
return 0
elif sum_nums == target:
return sol
sum_nums = 0
left_index = 0
# Regular Processing
for index in range(len_nums):
num = nums[index]
if sum_nums < target:
sum_nums += num
if sum_nums >= target:
sol = min(sol, index - left_index + 1)
else:
sum_nums += num
while sum_nums >= target:
sum_nums -= nums[left_index]
left_index += 1
left_index -= 1
sum_nums += nums[left_index]
sol = min(sol, index - left_index + 1)
while sum_nums >= target:
sum_nums -= nums[left_index]
left_index += 1
left_index -= 1
sum_nums += nums[left_index]
sol = min(sol, index - left_index + 1)
return sol