我正在尝试使用 bisect 实现最长递增子序列的迭代解决方案。我的实施在某些时候失败了。帮我修一下。
执行:
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()
预期输出:
[6, 7, 24, 457]
生成的输出:
[5, 7, 22, 72]