forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfLIS.java
More file actions
28 lines (26 loc) · 791 Bytes
/
LengthOfLIS.java
File metadata and controls
28 lines (26 loc) · 791 Bytes
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
package str;
public class LengthOfLIS {
public int lengthOfLIS(int[] nums){
if (nums.length == 0) return 0;
int[] dp = new int[nums.length];
dp[0] = 1;
int maxans = 1;
for (int i = 1; i < dp.length; i++) {
int maxval = 0;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]){
maxval = Math.max(maxval,dp[j]);
}
}
dp[i] = maxval + 1;
maxans = Math.max(maxans,dp[i]);
}
return maxans;
}
public static void main(String[] args) {
int[] nums = {10,9,2,5,3,7,101,18};
LengthOfLIS lengthOfLIS = new LengthOfLIS();
int r = lengthOfLIS.lengthOfLIS(nums);
System.out.println(r);
}
}