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

    问题描述:

    Given an unsorted array of integers, find the number of longest increasing subsequence.

    Example 1:

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

    Example 2:

    Input: [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.
    

    Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.

    解题思路:

    可以用一个数组lens存储到这个数字位置最长的自增序列的长度,用一个数组cnt存储以这个数字为结尾的最长的递增序列的个数。

    更新lens比较好更新:比较nums[i] 和 nums[j]的大小。

    更新cnt时需注意,需要比较lens[i] 和 lens[j]的大小:

      注意此时前提是nums[j] < nums[i]

      1. 若lens[i] < lens[j] + 1 也就是说 lens[i] 记录的不为最长的,至少比lens[j] , nums[i]的序列要短

      此时我们更新lens[i] = lens[j] +1, cnt[i] = cnt[j]

      2.若lens[i] == lens[j] + 1,说明当前这个序列又是满足条件的最长子序列 所以 cnt[i] += cnt[j]

    代码:

    class Solution {
    public:
        int findNumberOfLIS(vector<int>& nums) {
            vector<int> lens(nums.size(), 1);
            vector<int> cnt(nums.size(), 1);
            int maxLen = 0;
            for(int i = 0; i < nums.size(); i++){
                for(int j = 0; j < i; j++){
                    if(nums[i] > nums[j]){
                        if(lens[i] == lens[j] + 1)
                            cnt[i] += cnt[j];
                        else if(lens[i] < lens[j] + 1){
                            lens[i] = lens[j]+1;
                            cnt[i] = cnt[j];
                        }
                    }
                }
                maxLen = max(maxLen, lens[i]);
            }
            int ret = 0;
            for(int i = 0; i <lens.size(); i++){
                if(lens[i] == maxLen)
                    ret += cnt[i];
            }
            return ret;
        }
    };
  • 相关阅读:
    Office 2007在安装过程中出错-解决办法
    Sql日期时间格式转换
    大型网站架构演变和知识体系
    作为一个web开发人员,哪些技术细节是在发布站点前你需要考虑到的
    Java 入门基础
    技术类面试、笔试题汇总
    怎样玩转千万级别的数据
    数据库性能优化:SQL索引
    最后一公里极速配送
    编辑-滴滴算法大赛算法解决过程
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9197364.html
Copyright © 2011-2022 走看看