-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path209MinSizeSubArraySum.py
66 lines (60 loc) · 1.7 KB
/
209MinSizeSubArraySum.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution(object):
def minSubArrayLen(self, target, nums):
"""
:type target: int
:type nums: List[int]
:rtype: int
"""
partial_sum , i = 0, 0
while partial_sum<target and i<len(nums):
partial_sum += nums[i]
i+=1
#if no solution
if partial_sum<target:
return 0
current_window=i
i=0
while i<len(nums):
partial_sum -= nums[i]
if partial_sum >= target:
current_window -=1
if i+current_window<len(nums):
partial_sum += nums[i+current_window]
i+=1
#last part of array
return current_window
test_arr = [1,2,3,4,5]
sol=Solution()
print(sol.minSubArrayLen(15, test_arr))
class Solution(object):
def minSubArrayLen(self, target, nums):
"""
:type target: int
:type nums: List[int]
:rtype: int
"""
partial_sum , i = 0, 0
while partial_sum<target and i<len(nums):
partial_sum += nums[i]
i+=1
#if no solution
if partial_sum<target:
return 0
current_window=i
i=0
while i+current_window<len(nums):
partial_sum -= nums[i]
if partial_sum >= target:
current_window -=1
else:
partial_sum += nums[i+current_window]
i+=1
window_size = current_window
#last part of array
while i<len(nums):
current_window-=1
partial_sum -= nums[i]
if partial_sum >= target:
window_size = current_window
i+=1
return window_size