/
NumberOfLIS.java
49 lines (42 loc) · 1.27 KB
/
NumberOfLIS.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
40
41
42
43
44
45
46
47
48
49
class NumberOfLIS {
//https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/
public int findNumberOfLIS(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}
int dp[] = new int[nums.length];
int dpWays[] = new int[nums.length];
int max = Integer.MIN_VALUE;
int count = 0;
//Arrays.fill(dp, 1);
Arrays.fill(dpWays, 1);
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j <= i; j++) {
if (nums[i] > nums[j]) {
if (dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
dpWays[i] = dpWays[j]
} else if (dp[i] == dp[j] + 1) {
dpWays[i] += dpWays[j]
}
}
}
}
for (int i = 0; i < dp.length; i++) {
System.out.print(dp[i] + " ");
}
System.out.println();
for (int i = 0; i < dp.length; i++) {
System.out.print(dpWays[i] + " ");
}
for (int i = 0; i < dp.length; i++) {
max = max < dp[i] ? dp[i] : max;
}
for (int i = 0; i < dp.length; i++) {
if (dp[i] == max) {
count += dpWays[i];
}
}
return count;
}
}