-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodility-longest-ascending-walk.py
63 lines (53 loc) · 1.75 KB
/
codility-longest-ascending-walk.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
# Author: Wouter Coppieters
def solution(N, A, B, C):
tokens = [[[0,-float('inf')]] for n in xrange(N)]
C = sorted([(item, index) for index, item in enumerate(C)])
maxlength = 0
to_remove = []
for attractiveness, index in C:
frm, to = A[index], B[index]
left_token, right_token = None, None
if frm == to:
best_token = None
to_remove = []
for token in tokens[frm]:
if token[1] < attractiveness:
if best_token:
if best_token[0] < token[0]:
best_token = token
else:
best_token = token
if best_token:
left_token = best_token
maxlength = max(maxlength, best_token[0] + 1)
else:
to_remove = []
best_token = None
for token in tokens[frm]:
if token[1] < attractiveness:
if best_token:
if best_token[0] < token[0]:
best_token = token
else:
best_token = token
if best_token:
left_token = best_token
maxlength = max(maxlength, best_token[0] + 1)
best_token = None
for token in tokens[to]:
if token[1] < attractiveness:
if best_token:
if best_token[0] < token[0]:
best_token = token
else:
best_token = token
if best_token:
right_token = best_token
maxlength = max(maxlength, best_token[0] + 1)
if left_token:
tokens[to].append([left_token[0] + 1, attractiveness])
tokens[frm] = filter(lambda x: x[1] >= attractiveness or x == left_token, tokens[frm])
if right_token:
tokens[to] = filter(lambda x: x[1] >= attractiveness or x == right_token, tokens[to])
tokens[frm].append([right_token[0] + 1, attractiveness])
return maxlength