Description
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example
Example 1:1
2
3Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:1
2
3Input: [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.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
Solution
Add one array to record the number of each dp[i]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
28class Solution {
public int findNumberOfLIS(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int[] dp = new int[nums.length];
int[] count = new int[nums.length];
for (int i = 0; i < nums.length; i++){
dp[i] = 1;
count[i] = 1;
}
int maxLen = dp[0];
int res = 0;
for (int i = 1; i < nums.length; i++){
for (int j = 0; j < i; j++){
if (nums[j] < nums[i]){
if (dp[i] == dp[j] + 1) count[i] += count[j];
if (dp[i] < dp[j] + 1){
dp[i] = dp[j] + 1;
count[i] = count[j];
}
}
}
maxLen = Math.max(maxLen, dp[i]);
}
for(int i = 0; i < dp.length; i++)
if (dp[i] == maxLen) res += count[i];
return res;
}
}