-
Notifications
You must be signed in to change notification settings - Fork 0
/
102.py
47 lines (37 loc) · 1.22 KB
/
102.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
"""
Problem:
Given a list of integers and a number K, return which contiguous elements of the list
sum to K.
For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return [2, 3, 4].
"""
from typing import List, Optional
def get_arr_contiguous_sum(arr: List[int], k: int) -> Optional[List[int]]:
length = len(arr)
total_sum = 0
start, end = 0, 0
# generating the sequence using moving window
for i in range(length):
if total_sum == k:
return arr[start:end]
total_sum += arr[i]
end = i + 1
if total_sum > k:
total_sum -= arr[start]
start += 1
if total_sum == k:
return arr[start:end]
return None
if __name__ == "__main__":
print(get_arr_contiguous_sum([1, 2, 3, 4, 5], 0))
print(get_arr_contiguous_sum([1, 2, 3, 4, 5], 1))
print(get_arr_contiguous_sum([1, 2, 3, 4, 5], 5))
print(get_arr_contiguous_sum([5, 4, 3, 4, 5], 12))
print(get_arr_contiguous_sum([5, 4, 3, 4, 5], 11))
print(get_arr_contiguous_sum([1, 2, 3, 4, 5], 9))
print(get_arr_contiguous_sum([1, 2, 3, 4, 5], 3))
print(get_arr_contiguous_sum([1, 2, 3, 4, 5], 300))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""