-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlongest-increasing-subsequence.py
110 lines (78 loc) · 2.28 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Dynamic programming Python implementation
# of LIS problem
# lis returns length of the longest
# increasing subsequence in arr of size n
def lis_n_squared(arr):
n = len(arr)
# Declare the list (array) for LIS and
# initialize LIS values for all indexes
lis = [1] * n
# Compute optimized LIS values in bottom up manner
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
# Initialize maximum to 0 to get
# the maximum of all LIS
maximum = 0
# Pick maximum of all LIS values
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
# end of lis function
# Driver program to test above function
arr = [10, 22, 9, 33, 21, 50, 41, 60]
print("Length of lis is", lis_n_squared(arr))
# Python program to find
# length of longest
# increasing subsequence
# in O(n Log n) time
# Binary search (note
# boundaries in the caller)
# A[] is ceilIndex
# in the caller
def CeilIndex(A, l, r, key):
while r - l > 1:
m = l + (r - l) // 2
if A[m] >= key:
r = m
else:
l = m
return r
def LongestIncreasingSubsequenceLength(A, size):
# Add boundary case,
# when array size is one
tailTable = [0 for i in range(size + 1)]
len = 0 # always points empty slot
tailTable[0] = A[0]
len = 1
for i in range(1, size):
if A[i] < tailTable[0]:
# new smallest value
tailTable[0] = A[i]
elif A[i] > tailTable[len - 1]:
# A[i] wants to extend
# largest subsequence
tailTable[len] = A[i]
len += 1
else:
# A[i] wants to be current
# end candidate of an existing
# subsequence. It will replace
# ceil value in tailTable
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
return len, pop_zeros(tailTable)
def pop_zeros(items):
while items[-1] == 0:
items.pop()
return items
# Driver program to
# test above function
A = [2, 5, 3, 7, 11, 8, 10, 13, 6]
n = len(A)
print(
"Length of Longest Increasing Subsequence is ",
LongestIncreasingSubsequenceLength(A, n),
)
# This code is contributed
# by Anant Agarwal.