There was an error while loading. Please reload this page.
1 parent 3670680 commit 063f23aCopy full SHA for 063f23a
Dynamic Programming/673_Number_of_Longest_Increasing_Subsequence.java
@@ -0,0 +1,35 @@
1
+class Solution {
2
+ public int findNumberOfLIS(int[] nums) {
3
+ if (nums == null || nums.length == 0) {
4
+ return 0;
5
+ }
6
+
7
+ int[] dp = new int[nums.length], cnt = new int[nums.length];
8
+ Arrays.fill(dp, 1);
9
+ Arrays.fill(cnt, 1);
10
11
+ int result = 0, max = 1;
12
13
+ for (int i = 0; i < nums.length; i++) {
14
+ for (int j = 0; j < i; j++) {
15
+ if (nums[j] < nums[i]) {
16
+ if (dp[j] + 1 == dp[i]) {
17
+ cnt[i] += cnt[j];
18
+ } else if (dp[j] + 1 > dp[i]) {
19
+ dp[i] = dp[j] + 1;
20
+ cnt[i] = cnt[j];
21
22
23
24
25
+ if (dp[i] == max) {
26
+ result += cnt[i];
27
+ } else if (dp[i] > max) {
28
+ max = dp[i];
29
+ result = cnt[i];
30
31
32
33
+ return result;
34
35
+}
0 commit comments