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

class Solution {
    public int findNumberOfLIS(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp,1); // dp[i] 以i结尾的最大长度
        int[] count = new int[n]; // count[i] 以i结尾的最大长度的个数
        Arrays.fill(count,1);
        int max = 1;
        for(int i = 0; i < n; i++) {
            for(int j = i - 1; j >= 0; j--) {
                if(nums[i] > nums[j]) {
                    if(dp[j] + 1 > dp[i]) { //说明是新的最大长度 所以dp[i]count[i]都要更新
                        dp[i] = dp[j] + 1;
                        count[i] = count[j];
                    } else if (dp[j] + 1 == dp[i]) {
                        count[i] += count[j];
                    }
                }
            }
            max = Math.max(max,dp[i]);
        }
        int res = 0;
        for(int i = 0; i < n; i++) {
            if(dp[i] == max) res += count[i];
        }
        return res;
    }
}

 

posted @ 2020-08-19 19:09  Sexyomaru  阅读(85)  评论(0)    收藏  举报