-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_inc_seq.py
More file actions
44 lines (28 loc) · 1.24 KB
/
longest_inc_seq.py
File metadata and controls
44 lines (28 loc) · 1.24 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
44
"""Given an array of numbers, find the length of the longest increasing subsequence in the array.
The subsequence does not necessarily have to be contiguous.
For example, given the array [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15],
the longest increasing subsequence has length 6: it is 0, 2, 6, 9, 11, 15."""
def lis_v1(arr, n, max_len=1):
"""Recursive function to find longest increasing subsequence in the given array."""
max_end = 1
if n == 1:
return 1
for i in range(1, n):
res = lis_v1(arr, i, max_len)
if arr[i - 1] < arr[n - 1] and max_end < res + 1:
max_end = res + 1
max_len = max(max_len, max_end)
return max_len
def lis_v2(arr, n):
"""Function based on Dynamic Programming to return longest increasing subsequence."""
lis = [1] * n
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
max_len = 0
for i in range(n):
max_len = max(max_len, lis[i])
return max_len
seq = [0, 1, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 10, 11, 15]
print("Maximum increasing subsequence = ", lis_v2(seq, len(seq)))