## Longest Increasing Subsequence

Longest Increasing Subsequence (LIS) problems find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.

Sample Input:

8
186 186 150 200 160 130 197 220


Sample Output:

4


## Solution

State representation:

• f[i] is the length of LIS in a[1 ~ i] ending with a[i]

State calculation:

For each element “i”, we find the value of the LIS ending with that value. We can do this by finding the maximum LIS of all previous indexes (index “j”) where a[j] < a[i]. We can save the value of f[i] as the length of f[j] + 1, being the length of the LIS of j, and adding a[i] to the end of the sequence.