How can i get the longest sequence from a list of integers?


#1

The sequence should have the property: x[i] < x[i+1] < ... < x[i+p]


#2

Of consecutive numbers? Simple code with linear time complexity, but I had to use extra space:

def getLongestSequence(l):
    result = []
    temp = [l[0]]
    for i in xrange(1, len(l)):
        if(l[i] > temp[-1]):
            temp.append(l[i])
        else:
            if(len(temp) > len(result)):
                result = temp
            temp = [l[i]]
    if(len(temp) > len(result)):
        return temp
    return result

print getLongestSequence([0, 8, 4, 12, 2, 10, 6, 14, 1, 5, 9, 13, 3, 11, 7, 17, 19])

If any subsequence -> https://en.wikipedia.org/wiki/Longest_increasing_subsequence


#3

Thank you very much!!! <3


#4

You're very welcome :panda_face: