Write a function to determine longest increasing subsequence of a list of numbers given


Topic: Write a function to determine longest increasing subsequence of a list of numbers given

Solution

def longest_increaing_subsequence(myList):
    lis = [1] * len(myList)
    elements = [0] * len(myList)
    for i in range (1 , len(myList)):
        for j in range(0 , i):
            if myList[i] > myList[j] and lis[i]< lis[j] + 1:
                lis[i] = lis[j]+1
                elements[i] = j
    idx = 0
    maximum = max(lis)              
    idx = lis.index(maximum)
    seq = [myList[idx]]
    while idx != elements[idx]:
        idx = elements[idx]
        seq.append(myList[idx])
    return (maximum, reversed(seq))
	



List all Python Programs