-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfMaxSubIncreaseArray.java
More file actions
38 lines (35 loc) · 1009 Bytes
/
Copy pathLengthOfMaxSubIncreaseArray.java
File metadata and controls
38 lines (35 loc) · 1009 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
package dp;
import org.junit.Test;
/**
* @Author: wei1
* @Date: Create in 2018/12/10 0:41
* @Description: 求最长递增子序列的长度,==前一个比他小的结尾的子序列+1
* dp
*/
public class LengthOfMaxSubIncreaseArray {
public int lengthOfMaxSubIncreaseArray(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
int dp[] = new int[arr.length];
dp[0] = 1;
int result = 1;
for (int i = 1; i < arr.length; i++) {
int max = Integer.MIN_VALUE;
for (Integer integer : dp) {
if (integer < arr[i]) {
max = Math.max(integer, max);
}
}
dp[i] = max + 1;
result = Math.max(dp[i], result);
}
return result;
}
@Test
public void test() {
int[] arr = {5, -6, 4, 2, 0, -1, 3, 5};
int result = lengthOfMaxSubIncreaseArray(arr);
System.out.println(result);
}
}