# Longest Increasing Subsequence

### 描述​

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,

Given [10, 9, 2, 5, 3, 7, 101, 18],

The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n^2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

### 解法 1 动规​

• [10, 9, 2, 5, 3, 7, 101, 18]的答案，必然包含子问题 [10, 9, 2, 5, 3, 7] 的答案，即本题具有最优子结构性质
• [10, 9, 2, 5, 3, 7, 101, 18][10, 9, 2, 5, 3, 7] 都依赖 [10, 9, 2, 5, 3]这个子问题，即本题具有重叠子问题性质

j元素为终点的最长递增子序列，长度起码为1，因为最起码可以有一个j元素，单元素序列，就是本题的原子问题。

$f(j)=\begin{cases} 1 & j \geq 0\\ \max\left\{f(i)\right\}+1 & 0 \leq i < j \land nums[i] < nums[j] \end{cases}$

// Longest Increasing Subsequence// 时间复杂度O(n^2)，空间复杂度O(n)public class Solution {    public int lengthOfLIS(int[] nums) {        if (nums == null || nums.length == 0) return 0;        int[] dp = new int[nums.length];        Arrays.fill(dp, 1); // base case        int global = 1;        for (int j = 1; j < nums.length; ++j) {            for (int i = 0; i < j; ++i) {                if (nums[i] < nums[j]) {                    dp[j] = Math.max(dp[j], dp[i] + 1);                }            }            global = Math.max(global, dp[j]);        }        return global;    }}

### 解法 2 Insert Position​

// Longest Increasing Subsequence// 时间复杂度O(nlogn)，空间复杂度O(n)public class Solution {    public int lengthOfLIS(int[] nums) {        ArrayList<Integer> lis = new ArrayList<>();        for (int x : nums) {            int insertPos = lowerBound(lis, 0, lis.size(), x);            if (insertPos >= lis.size()) {                lis.add(x);            } else {                lis.set(insertPos, x);            }        }        return lis.size();    }    private static int lowerBound (ArrayList<Integer> A,                                   int first, int last, int target) {        while (first != last) {            int mid = first + (last - first) / 2;            if (target > A.get(mid)) first = ++mid;            else                 last = mid;        }        return first;    }}