美文网首页
673. Number of Longest Increasin

673. Number of Longest Increasin

作者: becauseyou_90cd | 来源:发表于2018-07-26 23:45 被阅读0次

    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;
    }
    

    }

    相关文章

      网友评论

          本文标题:673. Number of Longest Increasin

          本文链接:https://www.haomeiwen.com/subject/nvvimftx.html