I can't find anywhere solutions for my problem :(. Can someone help me and find (or write) this algorithm with comments? I can't do it myself. I spent on it at least 3 hours and nothing.
BIG Thank you!
I can't find anywhere solutions for my problem :(. Can someone help me and find (or write) this algorithm with comments? I can't do it myself. I spent on it at least 3 hours and nothing.
BIG Thank you!
Keeps a vector 'v[x] = y' where y is the smallest element of the original sequence that gives the longest increasing subsequence of length x. Originally you only have v[0] = -inf
.
Traverse the sequence for an element a
, look it up in the vector v
using binary search. You will not find a
exactly, but the largest x
such that v[x] <= a
(< if it is strictly increasing). So the longest increasing subsequence that ends at a will be of length x+1
. Now update v[x+1]
to be a (it can't be smaller than a, otherwise your search should have return x+1
), you may need to extend the vector.
The length of the LIS is the size of the vector. Keep track of which positions you used if you need to reconstruct the solution.