
Given an integer array nums, return the number of longest increasing subsequences.
Notice that the sequence has to be strictly increasing.
1
2
3
4
5
6
7
8
9
10
11
| Example 1:
Input: nums = [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: nums = [2,2,2,2,2]
Output: 5
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
|
Constraints:
- 1 <= nums.length <= 2000
- -10^6 <= nums[i] <= 10^6
Solution#
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
| class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
int[] len = new int[n];
int[] count = new int[n];
int res = 0;
int max = 0;
for (int i = 0; i < n; i++) {
len[i] = count[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
if (len[i] == len[j] + 1) {
count[i] += count[j];
} else if (len[i] < len[j] + 1) { // the same as in longest subsequence
len[i] = len[j] + 1; // len[i] = Math.max(len[i], len[j] + 1)
count[i] = count[j];
}
}
}
if (len[i] > max) {
max = len[i];
res = count[i];
} else if (len[i] == max) {
res += count[i];
}
}
return res;
}
}
|