-
Notifications
You must be signed in to change notification settings - Fork 17
/
odd-even-jump.py
66 lines (49 loc) · 1.89 KB
/
odd-even-jump.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
from functools import lru_cache
class Solution:
def next_higher_compute(self, arr):
stack = []
self._next_higher = list(range(len(arr)))
for _, pos in sorted(((n, p) for p, n in enumerate(arr))):
while stack and stack[-1] < pos:
self._next_higher[stack.pop()] = pos
stack.append(pos)
def next_lower_compute(self, arr):
stack = []
self._next_lower = list(range(len(arr)))
for _, pos in sorted(((-n, p) for p, n in enumerate(arr))):
while stack and stack[-1] < pos:
self._next_lower[stack.pop()] = pos
stack.append(pos)
def oddEvenJumps(self, A: List[int]) -> int:
self.next_higher_compute(A)
self.next_lower_compute(A)
dp = [[False, False] for _ in A]
dp[-1] = [True, True] # 0 - can reach end via odd jump, 1 - via even
for pos in reversed(range(len(A))):
if self._next_higher[pos] != pos:
dp[pos][0] = dp[self._next_higher[pos]][1]
if self._next_lower[pos] != pos:
dp[pos][1] = dp[self._next_lower[pos]][0]
return sum(p[0] for p in dp)
def oddEvenJumpsTopDown(self, A: List[int]) -> int:
@lru_cache(None)
def dfs(pos: int, odd: bool) -> bool:
"""
@param odd - True if odd, False if even
"""
if pos == len(A) - 1:
return True
if odd:
new_pos = self._next_higher[pos]
else:
new_pos = self._next_lower[pos]
if new_pos != pos:
return dfs(new_pos, not odd)
return False
self.next_higher_compute(A)
self.next_lower_compute(A)
good_indexes = 0
for pos in range(len(A)):
if dfs(pos, True):
good_indexes += 1
return good_indexes