Problem of the Day
A new programming or logic puzzle every Mon-Fri

Longest Incrementing Sequence

Given an array, please get the length of the longest incrementing sequence. The element order in the sequence should be same as the element order in the array. For example, in the array [1, 6, 3, 5, 9, 7], the longest incrementing sequence is 1, 3, 5, 7 and the length is 4. If there is a tie feel free to print any of the sequences.

Permalink: http://problemotd.com/problem/longest-incrementing-sequence/

Comments:

  • Nick Krichevsky - 10 years, 6 months ago

    A bit inefficient, but I got it in Python.

    array = [1, 6, 3, 5, 9, 7];
    sequences = []
    
    for item in array:
        l = []
        l.append(item)
        i = item
        lastPos = l.index(item)
        for i in range(max(array)):
            for number in array:
                if array.index(number)>lastPos:
                    if number==i and i>l[-1]:
                        lastPos=array.index(number)
                        l.append(number)
    
        sequences.append(l)
    sequences.sort()
    lastLen = -1
    for sequence in sequences:
        if lastLen != -1:
            if len(sequence)!=lastLen:
                break
        print sequence
        lastLen = len(sequence)
    

    reply permalink

Content curated by @MaxBurstein