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]
Your current algorithm doesn't seem to make much sense, as noted in BrenBarn's comment. Here is what I came up with: