-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_pointers.py
More file actions
42 lines (35 loc) · 808 Bytes
/
two_pointers.py
File metadata and controls
42 lines (35 loc) · 808 Bytes
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
import time
start = time.time()
arr = list(map(int, input().split()))
sum = int(input())
# def solve():
# start, end = 0, 0
# temp = 0
# cnt = 0
# while True:
# if start > len(arr) - 1 or end > len(arr) - 1:
# break
# for x in arr[start:end + 1]: temp += x
# if temp >= sum:
# start += 1
# if temp == sum:
# cnt += 1
# else:
# end += 1
# temp = 0
# return cnt
def solve():
end = 0
cnt = 0
temp = 0
for start in range(len(arr)):
while temp < sum and end < len(arr):
temp += arr[end]
end += 1
if temp == sum:
cnt += 1
temp -= arr[start]
return cnt
print(solve())
end = time.time()
print(end - start)