English 中文(简体)
不包括第一个和最后一项内容的所有亚拉里最大部分的概述
原标题:Sum of maximum element of all subarray not including the first and last element

我试图计算一个亚拉里每个最大部分的总和,但不包括第一个和最后一个部分。 冷漠态度是显而易见的,但我不想这样做。

至今还不清楚:

total = 0
arr = [4, 3, 5, 2, 8, 1]

我们首先从要素4和5来看,3 是唯一的编号,因此它是最大数目,因此到目前为止,总数是3。 接下来,我们只需要4和2,而两者之间的最大程度是5,因此,我们在总数中增加了。 我们用4、8和5,这仍然是最大的,因此,我们目前的总数现在为13个。 计算4和1时,我们最多有8个,因此,在目前为21个的总数字中,我们增加了8个。 移至3和2(因为3至5之间没有任何因素),最高比率为5,因此,我们增加了5。 从根本上说,我们只会在每一个可能的分支之间增加最大的内容。 我如何能够有效地做到这一点?

我已经尝试了显然效率低下的O(n^2)办法,我希望知道解决这一问题的更好办法。 也许通过打脚或点子? 我不清楚。 我正试图学习每日生活津贴,作为我即将在我的大学举办的“sa”课程的预先研究。

问题回答

就每个要素而言,我们可以计算最大的亚拉底,其中它是最大的要素(尽管与以前各要素的子拉底没有重叠,而后者可能与相邻的同等要素发生)。 有了这一信息,可以通过将每一要素与最大程度的分辨率相乘来计算答案。 在O(n)可以做到这一点,使用单一吨位的两处迭代。

C++实施实例:

#include <span>
#include <vector>
int solve(std::span<int> nums) {
    std::vector<int> leftLarger, stk;
    leftLarger.reserve(nums.size());
    stk.reserve(nums.size());
    for (int i = 0; i < ssize(nums); ++i) {
        while (!stk.empty() && nums[stk.back()] < nums[i]) stk.pop_back();
        leftLarger.push_back(stk.empty() ? 0 : stk.back());
        stk.push_back(i);
    }
    stk.clear();
    int ans = 0;
    for (int i = ssize(nums) - 1; ~i; --i) {
        while (!stk.empty() && nums[stk.back()] <= nums[i]) stk.pop_back();
        ans += nums[i] * ((stk.empty() ? ssize(nums) - 1 : stk.back()) - i) 
                       * (i - leftLarger[i]);
        stk.push_back(i);
    }
    return ans;
}

你们可以通过将各投入阵列的每个分支汇集起来来实现这一点,其中不包括第一个和最后几个要素,并找到每个分支的最大要素。 然后,将所有这些最大要素汇总起来。 这里是为了达到这一目的:

rel=“nofollow noreferer”>amazon





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签