forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum_size_subarray_sum.py
62 lines (56 loc) · 1.75 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
def minimum_subarray_sum(target: int, numbers: list[int]) -> int:
"""
Return the length of the shortest contiguous subarray in a list of numbers whose sum
is at least target. Reference: https://stackoverflow.com/questions/8269916
>>> minimum_subarray_sum(7, [2, 3, 1, 2, 4, 3])
2
>>> minimum_subarray_sum(7, [2, 3, -1, 2, 4, -3])
4
>>> minimum_subarray_sum(11, [1, 1, 1, 1, 1, 1, 1, 1])
0
>>> minimum_subarray_sum(10, [1, 2, 3, 4, 5, 6, 7])
2
>>> minimum_subarray_sum(5, [1, 1, 1, 1, 1, 5])
1
>>> minimum_subarray_sum(0, [])
0
>>> minimum_subarray_sum(0, [1, 2, 3])
1
>>> minimum_subarray_sum(10, [10, 20, 30])
1
>>> minimum_subarray_sum(7, [1, 1, 1, 1, 1, 1, 10])
1
>>> minimum_subarray_sum(6, [])
0
>>> minimum_subarray_sum(2, [1, 2, 3])
1
>>> minimum_subarray_sum(-6, [])
0
>>> minimum_subarray_sum(-6, [3, 4, 5])
1
>>> minimum_subarray_sum(8, None)
0
>>> minimum_subarray_sum(2, "ABC")
Traceback (most recent call last):
...
ValueError: numbers must be an iterable of integers
"""
if not numbers:
return 0
if target == 0 and target in numbers:
return 0
if not isinstance(numbers, (list, tuple)) or not all(
isinstance(number, int) for number in numbers
):
raise ValueError("numbers must be an iterable of integers")
left = right = curr_sum = 0
min_len = sys.maxsize
while right < len(numbers):
curr_sum += numbers[right]
while curr_sum >= target and left <= right:
min_len = min(min_len, right - left + 1)
curr_sum -= numbers[left]
left += 1
right += 1
return 0 if min_len == sys.maxsize else min_len