-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.java
More file actions
39 lines (35 loc) · 1.21 KB
/
LIS.java
File metadata and controls
39 lines (35 loc) · 1.21 KB
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 problems;
import java.util.Arrays;
/**
* 最长上升子序列(Longest Increasing Subsequence)
*/
public class LIS {
/**
* 最长递增子序列
* 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
* 子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
*
* https://leetcode-cn.com/problems/longest-increasing-subsequence/
*/
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
// dp数组中最大的值就是问题的解
int res = 0;
for (int i = 0; i < dp.length; i++) {
res = Math.max(res, dp[i]);
}
return res;
}
public static void main(String[] args) {
LIS lis = new LIS();
System.out.println(lis.lengthOfLIS(new int[]{10,9,2,5,3,7,101,18})); // 4
}
}