-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfLIS.java
More file actions
46 lines (38 loc) · 925 Bytes
/
Copy pathLengthOfLIS.java
File metadata and controls
46 lines (38 loc) · 925 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package DP;
import org.junit.Test;
import java.util.Arrays;
import java.util.Stack;
/**
* @author dekai.kong
* @difficult medium
* @create 2020-08-02 19:01
* @from
**/
public class LengthOfLIS {
public LengthOfLIS() {
}
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
Arrays.fill(dp,1);
for(int i =1;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);
}
}
}
int ans = 0;
for (int i = 0; i < dp.length; i++) {
ans = Math.max(dp[i],ans);
}
return ans;
}
@Test
public void test() {
// lengthOfLIS(new int[]{1,3,6,7,9,4,10,5,6});
lengthOfLIS(new int[]{10,9,2,5,3,7,101,18});
}
}