Skip to content

Commit 4476ec2

Browse files
authored
Create Solution.java
1 parent 9fe5b61 commit 4476ec2

File tree

1 file changed

+33
-0
lines changed
  • solution/0673.Number of Longest Increasing Subsequence

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int findNumberOfLIS(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
int n = nums.length;
7+
int[] dp = new int[n];
8+
int[] f = new int[n];
9+
Arrays.fill(dp, 1);
10+
Arrays.fill(f, 1);
11+
int max = 0;
12+
for (int i = 0; i < n; ++i) {
13+
for (int j = 0; j < i; ++j) {
14+
if (nums[i] > nums[j]) {
15+
if (dp[j] + 1 > dp[i]) {
16+
dp[i] = dp[j] + 1;
17+
f[i] = f[j];
18+
} else if (dp[j] + 1 == dp[i]) {
19+
f[i] += f[j];
20+
}
21+
}
22+
}
23+
max = Math.max(max, dp[i]);
24+
}
25+
int res = 0;
26+
for (int i = 0; i < n; ++i) {
27+
if (dp[i] == max) {
28+
res += f[i];
29+
}
30+
}
31+
return res;
32+
}
33+
}

0 commit comments

Comments
 (0)