Q673 最长递增子序列的个数

给定一个未排序的整数数组,找到最长递增子序列的个数。

示例 1:

输入: [1,3,5,4,7]
输出: 2
解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。

示例 2:

输入: [2,2,2,2,2]
输出: 5
解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。

注意: 给定的数组长度不超过 2000 并且结果一定是32位有符号整数。

class Solution {
    public int findNumberOfLIS(int[] nums) {
        if (nums == null || nums.length == 0)
            return 0;
        else if (nums.length == 1)
            return 1;

        int[] dp = new int[nums.length];
        int[] total = new int[nums.length];
        int max = 0;
        for(int i = 0; i < nums.length; i++) {
            int temp = 0;
            int count = 1;
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]) {
                    if (dp[j] > temp) {
                        temp = dp[j];
                        count = total[j];
                    } else if (dp[j] == temp) {
                        count += total[j];
                    }

                }
            }
            dp[i] = temp + 1;
            total[i] = count;
            max = dp[max] > dp[i] ? max : i;
        }

        int result = 0;
        for (int i = 0; i < nums.length; i++)
            result += dp[i] == dp[max] ? total[i] : 0;

        return result;
    }
}
posted @ 2019-04-24 21:51  围城DDD  阅读(188)  评论(0编辑  收藏  举报