-
Notifications
You must be signed in to change notification settings - Fork 0
/
209. ! Minimum Size Subarray Sum
52 lines (46 loc) · 1.31 KB
/
209. ! Minimum Size Subarray Sum
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
class Solution(object):
def minSubArrayLen(self, s, nums):
"""
:type s: int
:type nums: List[int]
:rtype: int
"""
# nums.sort(reverse=True)
# print(nums)
# sum = 0
# for i in range(len(nums)):
# sum += nums[i]
# if sum >= s:
# return i+1
# return 0
# the subarray should be contiguous!!
# res = 999999
# for i in range(len(nums)):
# j = i
# currS = 0
# while j < len(nums):
# currS += nums[j]
# j += 1
# if currS >= s:
# if j-i < res:
# res = j-i
# break
# if res == 999999:
# return 0
# else:
# return res
# overtime
# use 2 points
a, b, currS, res = 0, 0, 0, len(nums)+1
while b < len(nums):
while currS < s and b < len(nums):
currS += nums[b]
b += 1
while currS >= s and a < b:
res = min(res, b-a)
currS -= nums[a]
a += 1
if res == len(nums)+1:
return 0
else:
return res