673. Number of Longest Increasing Subsequence

Posted The Tech Road

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了673. Number of Longest Increasing Subsequence相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    int findNumberOfLIS(vector<int>& nums) {
        if (nums.size() == 0)   return 0;
        pair<int,int> res = {0, 0}; // <length,count>
        vector<pair<int,int>> dp(nums.size(), {1,1});  // pair: length, count
        for (int i = 0; i < nums.size(); i++) {
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]) {
                    if (dp[j].first + 1 == dp[i].first) {
                        dp[i].second += dp[j].second;
                    }
                    else if (dp[j].first + 1 > dp[i].first) {
                        dp[i].first = dp[j].first + 1;
                        dp[i].second = dp[j].second;
                    }
                }
            }
        }
        for (int i = 0; i < nums.size(); i++) {
            if (dp[i].first == res.first)
                res.second += dp[i].second;
            else if (dp[i].first > res.first) {
                res = dp[i];
            }
        }
        return res.second;
    }
};

 

以上是关于673. Number of Longest Increasing Subsequence的主要内容,如果未能解决你的问题,请参考以下文章

673. Number of Longest Increasing Subsequence

673. Number of Longest Increasing Subsequence

673. Number of Longest Increasing Subsequence

673. Number of Longest Increasing Subsequence

Leetcode-673 (Number of Longest Increasing Subsequence)最长递增子序列的个数

1. BinaryGap Find longest sequence of zeros in binary representation of an integer.