-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_increasing_subsequence.py
More file actions
43 lines (34 loc) · 1.25 KB
/
Copy pathlongest_increasing_subsequence.py
File metadata and controls
43 lines (34 loc) · 1.25 KB
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
"""program for finding the longest increasing sub-sequence"""
sequence = [10, 22, 9, 33, 21, 50, 41, 60, 80]
# sequence = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
length_of_lis = []
lis_of_length = {}
def initialize():
length = len(sequence)
while length > 0:
length_of_lis.append(1)
length -= 1
return
def longest_increasing_subsequence():
initialize()
lis_of_length[0] = [sequence[0]]
first_index = 1
while first_index < len(sequence):
second_index = 0
while second_index < first_index:
if sequence[first_index] > sequence[second_index] and length_of_lis[second_index] + 1 > length_of_lis[first_index]:
length_of_lis[first_index] = length_of_lis[second_index] + 1
ref_list = lis_of_length.get(second_index)
sub_sequence = ()
if ref_list is not None:
sub_sequence = list(ref_list)
sub_sequence.append(sequence[first_index])
lis_of_length[first_index] = sub_sequence
second_index += 1
first_index += 1
print sequence
print length_of_lis
print lis_of_length
return
if __name__ == '__main__':
longest_increasing_subsequence()