zoukankan      html  css  js  c++  java
  • leetcode 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.

    题目大意:求最长递增子序列的数量。

    方法一:动态规划

     1 class Solution {
     2 public:
     3     int findNumberOfLIS(vector<int>& nums) {
     4         int len = nums.size();
     5         vector<int> dp(len, 1); // dp[i] = length of longest ending in nums[i]
     6         vector<int> count(len, 1); // count[i] = number of longest ending in nums[i]
     7         int maxn = 0;
     8         for (int i = 0; i < len; i++) {
     9             for (int j = 0; j < i; j++) {
    10                 if (nums[i] > nums[j]) {
    11                     if (dp[j] >= dp[i]) {
    12                         dp[i] = dp[j] + 1;
    13                         count[i] = count[j];
    14                     } else {
    15                         if (dp[j] + 1 == dp[i])
    16                             count[i] += count[j];
    17                     }
    18                 }
    19             }
    20             maxn = max(maxn, dp[i]);
    21         }
    22         int cnt = 0;
    23         for (int i = 0; i < len; i++) {
    24             if (dp[i] == maxn)
    25                 cnt += count[i];
    26         }
    27         return cnt;
    28     }
    29 };

    方法二:线段树

    https://leetcode.com/articles/a-recursive-approach-to-segment-trees-range-sum-queries-lazy-propagation/

  • 相关阅读:
    算法
    日常
    算法
    算法
    算法
    算法
    NaviCat连接mysql出现加密方式错误的解决方案:
    Sql sugar的使用
    报表体联查详情页面
    第一次用临时表的感受:
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/11469124.html
Copyright © 2011-2022 走看看