Longest Increasing Subsequence (LIS)

Standard DP Algorithm.

O(n log n), using binary search. For LDS, change the for loop to run from i = N-1 ; i &gt;= 0 ; i --.

Java
int N = input.nextInt ; int[] arr = new int[N] ; int[] plz = new int[N+1] ; plz[0] = -1 ; for (int i = 0 ; i &lt; N ; i ++) {    arr[i] = input.nextInt ; plz[i+1] = Integer.MAX_VALUE ; } for (int i = 0 ; i &lt; N ; i ++) {    int hax = Arrays.binarySearch(plz, arr[i]); if (hax &lt; 0) hax = ~hax ; hax -- ; plz[hax] = arr[i] ; } int c = 0 ; for (int i = 1 ; i &lt;= N ; i ++) {    if (plz[i] &gt; plz[i-1] && plz[i] != Integer.MAX_VALUE ) {        c ++ ; }    else break ; } System.out.println(c);

Wikipedia
The algorithm outlined below solves the longest increasing subsequence problem efficiently, using only arrays and binary searching. For the first time this algorithm was developed by M. L. Fredman in 1975. It processes the sequence elements in order, maintaining the longest increasing subsequence found so far. Denote the sequence values as X[1], X[2], etc. Then, after processing X[i], the algorithm will have stored values in two arrays:
 * M[j] — stores the position k of the smallest value X[k] such that k ≤ i (note we have k ≤ j ≤ i here) and there is an increasing subsequence of length j ending at X[k]
 * P[k] — stores the position of the predecessor of X[k] in the longest increasing subsequence ending at X[k].

In addition the algorithm stores a variable L representing the length of the longest increasing subsequence found so far.

Note that, at any point in the algorithm, the sequence
 * X[M[1]], X[M[2]], ..., X[M[L]]

is nondecreasing. For, if there is an increasing subsequence of length i ending at X[M[i]], then there is also a subsequence of length i-1 ending at a smaller value: namely the one ending at P[M[i]]. Thus, we may do binary searches in this sequence in logarithmic time.

The algorithm, then, proceeds as follows. 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) The result of this is the length of the longest sequence in L. The actual longest sequence can be found by backtracking through the P array: the last item of the longest sequence is in X[M[L]], the second-to-last item is in X[P[M[L]]], etc. Thus, the sequence has the form
 * ..., X[P[P[M[L]]]], X[P[M[L]]], X[M[L]].