-3

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!

Matthew
  • 63
  • 10
  • No. That is not what SO is for, if you can't do it yourself then hire a free lancer.' – Epodax May 11 '16 at 07:57
  • I wrote it in O(n^2) without any problem, but I have problem in O(nlogn). For better programmer this will be 5 mins... – Matthew May 11 '16 at 08:10

1 Answers1

0

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.

Sorin
  • 11,863
  • 22
  • 26