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

1
2
3
Input: [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.

LIS,大名鼎鼎,初级dp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
if (n == 0) return 0;
int[] f = new int[n];
Arrays.fill(f, 1);

int res = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
f[i] = Math.max(f[i], f[j] + 1);
}
}
res = Math.max(res, f[i]);
}
return res;
}
}

O(n log n)方法: 还没写