I am trying to implement an iterative solution for Longest Increasing Subsequence using bisect. My implementation is failing at some point. Help me fix it.
Implementation:
from bisect import bisect
def lis_iterative(seq):
    buff = []
    for n in seq:
        i = bisect(buff, n)
        if i == len(buff):
            buff.append(n)
        else:
            buff[i] = n
    return buff
def main():
    seq = [43, 25, 6, 37, 27, 26, 7, 24, 457, 5, 22, 72]
    print lis_iterative(seq)
main()
Expected Output:
[6, 7, 24, 457]
Generated Output:
[5, 7, 22, 72]
buffis no longer a subsequence (since you put a later element in at an earlier position).bisect?bisectworks on sorted sequences.