-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetMaxsubarray_updated.py
More file actions
40 lines (33 loc) · 1.17 KB
/
Copy pathgetMaxsubarray_updated.py
File metadata and controls
40 lines (33 loc) · 1.17 KB
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
def getMaximumSubarray(arr):
n = len(arr)
if n == 1:
return 0
idx_1 = arr.index(1)
if idx_1 == 0 or idx_1 == n - 1:
return n - 1
# helper: expand an interval until it becomes a valid permutation
# validity condition for a permutation segment containing 1 is:
# max(segment) == len(segment)
def expand(L, R, direction):
current_max = 0
for i in range(L, R + 1):
if arr[i] > current_max:
current_max = arr[i]
while (R - L + 1) != current_max:
if direction == "left":
L -= 1
if arr[L] > current_max:
current_max = arr[L]
else: # direction == "right"
R += 1
if arr[R] > current_max:
current_max = arr[R]
return (R - L + 1)
kept1 = expand(0, idx_1, "right") # keep prefix, expand right
kept2 = expand(idx_1, n - 1, "left") # keep suffix, expand left
kept_len = min(kept1, kept2)
return n - kept_len
if __name__ == '__main__':
n = int(input().strip())
arr = [int(input().strip()) for _ in range(n)]
print(getMaximumSubarray(arr))