我遇到一个问题,需要编写一个算法来返回一个大数组中每个连续的k元素子数组的最大元素,并将这些最大元素读入到它们自己的数组中,如下所示:
Given int array = {3, 7, 20, 6, 12, 2, 0, 99, 5, 16}, and int k = 4,
--> creates the array {20, 20, 20, 12, 99, 99, 99}
[because there are 7 consecutive sub-arrays of size 4 within the given array:
{3, 7, 20, 6}, {7, 20, 6, 12}, {20, 6, 12, 2}, ... , {0, 99, 5, 16}
and the max element of these, respectively, is 20, 20, 20, ..., 99 which
are read into the resulting array.
现在我遇到的问题是:我知道如何使用O(n^2)复杂度实现它,但想要使其更快,即为O(n),或者如果不可能,则为O(nlog(n))。有人知道是否有更快的方法来做到这一点吗?如果有,请告诉我具体如何操作。
O(n^2)
中。 - Aiden Strydom