zoukankan      html  css  js  c++  java
  • 673. 最长递增子序列的个数

    • dp 解法 时间复杂度 O(N^2)
    * 假设对于以 nums[i] 结尾的序列,我们知道最长序列的长度 length[i],以及具有该长度的序列的 count[i]。
    * 对于每一个 j<i 和一个 nums[i]>nums[j],我们可以将一个 nums[i] 附加到以 nums[j] 结尾的最长子序列上。
    * 如果这些序列的长度 length[j]+1 > length[i],那么我们就知道现在有count[j]个这种长度(length[j]+1)的序列。如果这些序列的长度length[j]+1 == length[i],那么我们就知道现在有 count[j] 个额外的序列(即 count[i]+=count[j]。
    
    * The result is the sum of each count[i] while its corresponding length[i] is the maximum length.
    

    public int findNumberOfLIS(int[] nums) {
            int[] length = new int[nums.length];
            int[] count = new int[nums.length];
            for (int i = 0; i < nums.length; i++) {
                length[i] = 1;
                count[i] = 1;
                for (int j = 0; j < i; j++) {
                    if (nums[i] > nums[j]) {
                        if (length[j] + 1 > length[i]) {
                            length[i] = length[j] + 1;
                            count[i] = count[j];
                        } else if (length[j] + 1 == length[i]) {
                            count[i] += count[j];
                        }
                    }
                }
            }
    
            int maxLen = 0;
            for (int len : length) {
                maxLen = Math.max(maxLen, len);
            }
            int result = 0;
            for (int i = 0; i < length.length; i++) {
                if (length[i] == maxLen) {
                    result += count[i];
                }
            }
            return result;
        }
    
  • 相关阅读:
    2019.6.15刷题统计
    入门组完成情况
    2019.6.14刷题统计
    2019.6.13刷题统计
    绑定与非绑定方法 继承 继承与抽象 查找属性关系 派生与覆盖 访问父类的内容
    23 xml 面向对象
    day22 configparser模块 subprocsee模块 表格
    Python常用模块
    20.logging日志 re正则
    导入模块 包
  • 原文地址:https://www.cnblogs.com/lasclocker/p/11441998.html
Copyright © 2011-2022 走看看