动态规划19(Leetcode673最长递增子序列的个数)

有点绕

代码:

class Solution {
    public int findNumberOfLIS(int[] nums) {
        int n = nums.length;
        int maxLen = 0,ans = 0;
        int[] dp = new int[n];
        int[] cnt = new int[n];
        for(int i=0;inums[j]){
                    if(dp[j]+1>dp[i]){
                        dp[i] = dp[j]+1;
                        cnt[i]=cnt[j];
                    }else if(dp[j]+1==dp[i]){
                        cnt[i]+=cnt[j];
                    }
                }
            }
            if(dp[i]>maxLen){
                maxLen = dp[i];
                ans = cnt[i];
            }else if(dp[i]==maxLen){
                ans+=cnt[i];
            }
            
        }
        return ans;
    }
}

你可能感兴趣的:(动态规划,算法,leetcode)