-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy path975.py
More file actions
24 lines (20 loc) · 769 Bytes
/
975.py
File metadata and controls
24 lines (20 loc) · 769 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
class Solution:
def oddEvenJumps(self, A: List[int]) -> int:
n = len(A)
next_higher, next_lower = [0] * n, [0] * n
stack = []
for a, i in sorted([a, i] for i, a in enumerate(A)):
while stack and stack[-1] < i:
next_higher[stack.pop()] = i
stack.append(i)
stack = []
for a, i in sorted([-a, i] for i, a in enumerate(A)):
while stack and stack[-1] < i:
next_lower[stack.pop()] = i
stack.append(i)
higher, lower = [0] * n, [0] * n
higher[-1] = lower[-1] = 1
for i in range(n - 1)[::-1]:
higher[i] = lower[next_higher[i]]
lower[i] = higher[next_lower[i]]
return sum(higher)