zoukankan      html  css  js  c++  java
  • 673. Number of Longest Increasing Subsequence

    Given an integer array nums, return the number of longest increasing subsequences.

    Notice that the sequence has to be strictly increasing.

    Example 1:

    Input: nums = [1,3,5,4,7]
    Output: 2
    Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
    

    Example 2:

    Input: nums = [2,2,2,2,2]
    Output: 5
    Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
    
    

    Constraints:

    • 1 <= nums.length <= 2000
    • -106 <= nums[i] <= 106

    dp, time = O(n ^ 2), space = O(n)

    class Solution {
        public int findNumberOfLIS(int[] nums) {
            int n = nums.length;
            if(n <= 1) {
                return n;
            }
            int res = 0, maxLen = 0;
            int[] lengths = new int[n]; //lengths[i] = length of longest ending in nums[i]
            int[] counts = new int[n];  //count[i] = number of longest ending in nums[i]
    
            for(int i = 0; i < n; i++) {
                lengths[i] = counts[i] = 1;
                for(int j = 0; j < i; j++) {
                    if(nums[j] < nums[i]) {
                        if(lengths[j] + 1 == lengths[i]) {
                            counts[i] += counts[j];
                        }
                        if(lengths[j] + 1 > lengths[i]) {
                            counts[i] = counts[j];
                            lengths[i] = lengths[j] + 1;
                        }
                    }
                }
                if(lengths[i] == maxLen) {
                    res += counts[i];
                }
                if(lengths[i] > maxLen) {
                    maxLen = lengths[i];
                    res = counts[i];
                }
            }
            return res;
        }
    }
  • 相关阅读:
    [转]PYTHON-SCRAPY-WINDOWS下的安装笔记
    [转]Scrapy入门教程
    [转]Centos 6.5 安装 Scrapy 0.22.2成功
    Python的三个常用内置函数
    sublime连接Python的使用
    tornado-options(3)
    配置 PPP 封装和认证
    交换机验证 PVST 实验
    vlan间通信配置vtp模式
    convert expdp dmp file to SQL DDL statements
  • 原文地址:https://www.cnblogs.com/fatttcat/p/13959449.html
Copyright © 2011-2022 走看看