문제

Here is the pseudo code for longest increasing sub sequence given on 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)

I have understood how the code works. The only thing i cannot understand is the necessity of this statement (if j == L or X[i] < X[M[j+1]]:) I have tried running the algorithm on many examples and what i could make out is that in all the cases either j == L or X[i] < X[M[j+1]] and so the if statement always evaluates to True. Could you give me an example where the if loop is false and thus required for the algorithm ??

도움이 되었습니까?

해결책

When there are duplicates the if condition will fail

Consider X={2, 2, 2}

if Condition fails when j=0 and L=1

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top