-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSolution300.java
39 lines (34 loc) · 982 Bytes
/
Solution300.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 最长上升子序列
* @date: 2018/08/22
*/
public class Solution300 {
public static void main(String[] args) {
int[] nums = {10, 9, 2, 5, 3, 7, 101, 18};
Solution300 test = new Solution300();
System.out.println(test.lengthOfLIS(nums));
}
public int lengthOfLIS(int[] nums) {
if (null == nums || 0 >= nums.length) {
return 0;
}
int ans = Integer.MIN_VALUE;
// dp[i]表示:以第i个位置结尾的最长上升子序列
int[] dp = new int[nums.length];
for (int i = 0; i < nums.length; ++i) {
int mx = 1;
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
mx = Math.max(mx, dp[j] + 1);
}
}
dp[i] = mx;
if (dp[i] > ans) {
ans = dp[i];
}
}
return ans;
}
}