-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_increasing_subsequence.py
64 lines (51 loc) · 1.49 KB
/
longest_increasing_subsequence.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
# f(i) - length of the longest subseq ending on A[i]
def ps(A, P, i): # parent searching
if P[i] != -1:
ps(A, P, P[i])
print(A[i])
def lis(A):
n = len(A)
F = [1] * n
#P = [-1] * n
for i in range(1, n):
for j in range(i):
if A[i] > A[j] and F[j] + 1 > F[i]:
#P[i] = j
F[i] = F[j] + 1
# ps(A, P, 8)
return max(F)#, P
# https://cp-algorithms.com/sequences/longest_increasing_subsequence.html#solution-in-on-log-n-with-dynamic-programming-and-binary-search
def lis_v2(A):
n = len(A)
last = [float('inf') for _ in range(n + 1)]
last[0] = float('-inf')
for i in range(n):
for j in range(1, n + 1):
if last[j - 1] < A[i] < last[j]:
last[j] = A[i]
print(last)
for i in range(n, 0, -1):
if last[i] < float('inf'):
return i
return 0
### nlogn
def binary_search(A, val): # returns first greater than val
left_idx = 0
right_idx = len(A) - 1
while left_idx <= right_idx:
mid_idx = (left_idx + right_idx) //2
if val > A[mid_idx]:
left_idx = mid_idx + 1
else:
right_idx = mid_idx - 1
return left_idx
def lis_v2_nlogn(A):
n = len(A)
last = [float('inf') for _ in range(n + 1)]
last[0] = float('-inf')
for i in range(n):
idx = binary_search(last, A[i])
last[idx] = A[i]
A = [2, 1, 4, 3, 1, 5, 2, 7, 8, 3]
A = [3, 1, 5, 7, 2, 4, 9, 3, 17, 3]
print(lis_v2(A))