https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/
class Solution {
public int findNumberOfLIS(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int len = nums.length;
int[] dp = new int[len];
int[] sum = new int[len];
Arrays.fill(dp, 1); Arrays.fill(sum, 1);
int res = 1, maxLen = 1;
for(int i = 1; i < len; i++){
for(int j = i - 1; j >= 0; j--){
if(nums[i] > nums[j]){
if(dp[i] == dp[j] + 1)
sum[i] += sum[j];
if(dp[i] < dp[j] + 1){
dp[i] = dp[j] + 1;
sum[i] = sum[j];
}
}
}
if(maxLen == dp[i]) res += sum[i];
if(maxLen < dp[i]){
maxLen = dp[i];
res = sum[i];
}
}
return res;
}
}
网友评论