【问题标题】:Length of longest increasing subsequence, O(n) complexity最长递增子序列的长度,O(n) 复杂度
【发布时间】:2018-08-26 16:59:53
【问题描述】:

此函数是否返回数组 A[] 中存在的递增子序列的最大长度?

int maxLength(int A[], int n) { // n - size of A[]

   int subRes[n];

   subRes[0] = 1;

   for (int i = 1; i < n; i++) {

       subRes[i] = (A[i] > A[i-1] ? subRes[i-1] + 1 : 1);
   }

   int maxL = 0;
   for (int i = 0; i < n; i++) maxL = max(maxL, subRes[i]);

   return maxL;
}

【问题讨论】:

标签: algorithm dynamic-programming


【解决方案1】:

上述解决方案不正确。上面的代码是针对最长递增序列而不是子序列。对于最长递增子序列检查 https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/

【讨论】:

    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多