最长递增子序列的个数
public static int findNumberOfLISolution(int[] nums) {
int n = nums.length;
if(n == 1) return 1;//边界情况
int[] dp = new int[n];//以nums[i]结尾的最长子序列的长度
int[] count = new int[n];//以nums[i]结尾的最长子序列的个数
//初始化
Arrays.fill(dp, 1);
Arrays.fill(count, 1);
int maxLength = 0;//记录最长序列的长度
//完成状态转移dp[],count[]
for(int i = 1; i < n; i++){
for(int j = 0; j < i; j++){
if(nums[i] > nums[j]){
if(dp[j] + 1 > dp[i]){//表示最大长度进行了更新,那么count[i] = count[j]
dp[i] = dp[j] + 1;
count[i] = count[j];
}else if(dp[j] + 1 == dp[i]){//表示有重复的长度出现,则count[i]+=count[j]
count[i] += count[j];
}
}
}
//dp[i]在0<=j<i中是动态更新的,将取一轮中的最大值dp[i]与所记录的maxlength进行比较更新。
maxLength = Math.max(maxLength, dp[i]);
}
int res = 0;
for(int i = 0; i < n; i++){
if(dp[i] == maxLength){
res += count[i];
}
}
return res;
}
查看11道真题和解析