2022年4月27日 作者 zeroheart

算法基础第16天

673. 最长递增子序列的个数


/**
673. 最长递增子序列的个数
给定一个未排序的整数数组 nums , 返回最长递增子序列的个数 。

注意 这个数列必须是 严格 递增的。



示例 1:

输入: [1,3,5,4,7]
输出: 2
解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。
示例 2:

输入: [2,2,2,2,2]
输出: 5
解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。


提示:

1 <= nums.length <= 2000
-10^6 <= nums[i] <= 10^6

* @author zeroheart
*
*/
public class Question673 {
public static int findNumberOfLIS(int[] nums) {
int res = 0;
int max = 0;
int len = nums.length;

int[] dp = new int[len];
int[] count = new int[len];

Arrays.fill(dp, 1);
Arrays.fill(count, 1);

for(int i = 0; i<len; i++){
for(int j = 0; j<i; j++){
if(nums[i] > nums[j]){
if(dp[j] + 1 == dp[i]){
count[i] += count[j];
}else if(dp[j] + 1 > dp[i]){
count[i] = count[j];
dp[i] = dp[j] + 1;
}
}
}
max = Math.max(dp[i], max);
}

for (int i = 0; i < count.length; i++) {
if(dp[i] == max){
res += count[i];
}
}

return res;
}

public static void main(String[] args) {
System.out.println(findNumberOfLIS(new int[]{1,3,5,4,7}));
}




}