zoukankan      html  css  js  c++  java
  • Java实现 LeetCode 673 最长递增子序列的个数(递推)

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

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

    示例 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位有符号整数。

    PS:
    普通递推,加一个记录的数组

    class Solution {
       public int findNumberOfLIS(int[] nums) {
    
           
            if (nums.length == 0) {
                return 0;
            }
    
            int[] dp = new int[nums.length];
            int[] combination = new int[nums.length];
    
            Arrays.fill(dp, 1);
            Arrays.fill(combination, 1);
    
            int max = 1, res = 0;
    
            for (int i = 1; i < dp.length; i++) {
                for (int j = 0; j < i; j++) {
                    if (nums[i] > nums[j]) {
                        if (dp[j] + 1 > dp[i]) {  
                            dp[i] = dp[j] + 1;
                            combination[i] = combination[j];
                        } else if (dp[j] + 1 == dp[i]) {  
                            combination[i] += combination[j];
                        }
                    }
                }
                max = Math.max(max, dp[i]);
            }
    
            for (int i = 0; i < nums.length; i++)
                if (dp[i] == max) res += combination[i];
    
            return res;
        }
    }
    
  • 相关阅读:
    websocket1
    webpack 入门三
    weboack 入门二
    webpack 入门一
    输入一个url发生了什么
    http详解
    JavaScript对象详解
    javaScript垃圾回收机制
    js数据类型与隐式类型转换
    iOS证书申请、AppStore上架流程
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13074817.html
Copyright © 2011-2022 走看看