最长的子序列

时间:2013-07-05 07:04:01

标签: algorithm lis

以下是Wikipedia

上给出的最长增长子序列的伪代码
L = 0
 for i = 1, 2, ... n:
    binary search for the largest positive j ≤ L
    such that X[M[j]] < X[i] (or set j = 0 if no such value exists)
    P[i] = M[j]
    if j == L or X[i] < X[M[j+1]]:
       M[j+1] = i
       L = max(L, j+1)

我已经理解了代码是如何工作的。我唯一不能理解的是这个陈述的必要性(如果j == L或X [i]

1 个答案:

答案 0 :(得分:3)

当存在重复时,if条件将失败

考虑X={2, 2, 2}

if条件在j=0L=1

时失败
相关问题