-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1477_Find_Two_Non-overlapping_Subarrays_Each_with_Target_Sum.py
76 lines (66 loc) · 2.37 KB
/
1477_Find_Two_Non-overlapping_Subarrays_Each_with_Target_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
result = []
pre = [float(inf) for _ in range(len(arr))]
post = [float(inf) for _ in range(len(arr))]
total = 0
left = 0
for right, num in enumerate(arr):
total += num
if total == target:
result.append((left, right))
if left < len(arr) - 1:
total -= arr[left]
left += 1
elif total > target:
total = total - arr[left]
left += 1
while left < right and total > target:
total -= arr[left]
left += 1
if total == target:
result.append((left, right))
total -= arr[left]
left += 1
if len(result) < 2:
return -1
start = result[0][1]
cur_length = float(inf)
for item in result:
cur_length = min(cur_length, item[1] - item[0] + 1)
for i in range(start, item[1] + 1):
pre[i] = cur_length
start = item[1] + 1
for i in range(start, len(arr)):
pre[i] = cur_length
start = result[-1][0]
cur_length = result[-1][1] - result[-1][0] + 1
for item in result[::-2]:
for i in range(item[0] + 1, start + 1):
post[i] = cur_length
start = item[0]
cur_length = min(cur_length, item[1] - item[0] + 1)
for i in range(0, start + 1):
post[i] = cur_length
min_length = float(inf)
for item in result:
if item[0] == 0 and item[1] == len(arr) - 1:
return -1
elif item[0] - 1 < 0:
side = post[item[1] + 1]
elif item[1] + 1 == len(arr):
side = pre[item[0] - 1]
else:
side = min(pre[item[0] - 1], post[item[1] + 1])
min_length = min((item[1] - item[0] + 1) + side, min_length)
if min_length == 6:
print(item)
if min_length == float(inf):
return -1
else:
return min_length
'''
comments:
1. how to get sum of a continuous subarray and move left and right point correspondingly
2. the index to assign values to the pre and post
'''