-
Notifications
You must be signed in to change notification settings - Fork 222
/
poisonous-plants.py
58 lines (44 loc) · 1.31 KB
/
poisonous-plants.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
#!/bin/python3
import sys
def do_day(arr):
prev = arr[0]
res = [arr[0]]
for el in arr[1:]:
temp = el
if el <= prev:
#print("{} > {}".format(el, prev))
#arr.remove(el)
res.append(el)
prev = temp
return res
def poisonousPlants_brute(arr):
res = 0
arr_prev = []
arr_cur = arr
while arr_cur != arr_prev:
#print("cur = {} prev = {}".format(arr_cur, arr_prev))
arr_cur, arr_prev = do_day(list(arr_cur)), arr_cur
res += 1
return res - 1
def poisonousPlants(arr):
stack = [0]
days = [0] * len(arr)
pivot = arr[0]
res = 0
for ind in range(1, len(arr)):
if arr[ind] > arr[ind - 1]:
days[ind] = 1
pivot = min(pivot, arr[ind])
#print("stack = {}".format(stack))
while stack and arr[stack[-1]] >= arr[ind]:
if arr[ind] > pivot:
days[ind] = max(days[ind], days[stack[-1]] + 1)
stack.pop()
res = max(res, days[ind])
stack.append(ind)
return res
if __name__ == "__main__":
n = int(input().strip())
p = list(map(int, input().strip().split(' ')))
result = poisonousPlants(p)
print(result)