【问题标题】:Maximum sum of strictly increasing susequence of size 4大小为 4 的严格递增子序列的最大和
【发布时间】:2021-04-01 12:43:09
【问题描述】:

有没有更有效的方法来获得大小为 4 的严格递增子序列的最大总和?

我使用了 DP 其中DP[i][j] = max(DP[k][j-1]) 这样k < iA[k] < A[i], j < 4, A 是数组。 此解的时间复杂度为 n^2。

我想降低时间复杂度。

设数组为 1, 10, 6, 8, 9, 11, 9, 9, 13 那么答案是 13 + 11 + 9 + 8

【问题讨论】:

    标签: c++ arrays c algorithm dynamic-programming


    【解决方案1】:

    对于从 1 到 4 的每个 i,通过递减最大值和求和来保持您已经找到的最佳选项。 (您可以为此使用跳过列表、二叉树或其他任何东西。)这将严格按照最大 AND 和进行降序。

    那么你的更新逻辑是这样的:

    for i in [1, 2, 3, 4]:
        for j in your array:
            find for i-1 the best candidate whose max is < j:
            if found:
                create new candidate by adding j to that one
                look for best candidate of size i with max smaller than or equal to this
                if not found or its sum is < this one:
                    insert new candidate into data structure
                    delete from data structure any existing elements whose max >= this and whose sum is <= this.
    

    每次查找都是O(log(n))。插入和删除也是。对于每个i,对于每个元素,我们最多执行 2 次查找、1 次插入,稍后可能会执行删除。对于时间O(k n log(n)),其中 k 是您正在寻找的递增链的长度。 (在你的情况下,4。)

    【讨论】:

    • 我无法理解这种方法。请详细说明。
    • 这里的最大值是多少?
    • @DwaipayanBarman max 是升序的最大值。和是升序的总和。关键的见解是,如果在任何时候有两个序列,其中 A 的最大值 >= B,总和
    • 所以重点是“丢弃不能尽快达到最优的序列,将可能导致最优的序列保留在易于搜索的排序数据结构中”。
    • 谢谢,现在知道了。
    猜你喜欢
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    相关资源
    最近更新 更多