最长递增子序列的个数>>>
在leetcode300
的基础上进行加强,求最长子序列的组合,添加个数组即可
package BDyNamicProgramming;
import DString.Problem3;
import java.util.Arrays;
public class Problem673 {
public int findNumberOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
int[] combination = new int[nums.length];
Arrays.fill(dp, 1);
Arrays.fill(combination, 1);
int max = 1, res = 0;
for (int i = 1; i < dp.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
if (dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
combination[i] = combination[j];
} else if (dp[j] + 1 == dp[i]) {
combination[i] += combination[j];
}
}
}
max = Math.max(max, dp[i]);
}
for (int i = 0; i < nums.length; i++)
if (dp[i] == max) res += combination[i];
return res;
}
public static void main(String[] args) {
int[] arr = {2,2,2,2,2};
Problem673 problem673 = new Problem673();
int size = problem673.findNumberOfLIS(arr);
System.out.println(size);
}
}