longest increasing subsequence(O(nlogn))

前端 未结 8 831
花落未央
花落未央 2020-12-07 11:10

LIS:wikipedia

There is one thing that I can\'t understand:

why is X[M[i]] a non-decreasing sequence?

相关标签:
8条回答
  • 2020-12-07 12:10

    The base idea behind algorithm is to keep list of LIS of a given length ending with smallest possible element. Constructing such sequence

    1. Find immediate predecessor in already known last elements sequence ( lets say its of length k)
    2. Try to append current element to this sequence and build new better solution for k+1 length

    Because in first step you search for smaller value then X[i] the new solution (for k+1) will have last element greater then shorter sequence.

    I hope it will help.

    0 讨论(0)
  • 2020-12-07 12:14

    There is a proof here https://strncat.github.io/jekyll/update/2019/06/25/longest-increasing-subsequence.html

    basically it is impossible to not be a strictly increasing subsequence. The proof is by contradiction: Suppose it is not then we have two cases: Case 1) There is some element M[j] that ends two subsequences of length j and j+some number. This is impossible (proof in link)

    Case 2) Slightly different that Case 1 but pretty the same reasoning. How can you have a smallest number end two subsequences of two different lengths? it can't be.

    0 讨论(0)
提交回复
热议问题