Longest increasing subsequence

Here is a simple way find the longest increasing subsequence of an n element array A.


 * make a sorted copy of A named B: cost $$O(n \lg(n))$$
 * run Longest Common Subsequence on and A and B: cost $$O(n^2)$$
 * total complexity: $$O(n^2)$$

There is also a simple direct way to compute the LIS in $$O(n^2)$$ time. The simple direct algorithm can be improved to run in $$O(n \lg n)$$ time.

A direct way is storing the best increasing subsequence of every length as you proceed into the original sequence. By 'best' we mean the one of that length with the lowest last number. So for every one of these sequences there or two possibilities to consider when one considers the following number of the original sequence:
 * The number is lower than the last number of the subsequence and greater than the second last. In this case the last number of the subsequence is replaced by the new number.
 * The number is greater than the last number of the subsequence: The number is appended to the subsequence and if this subsequence is the best subsequence with this length, it will be stored.