大きな配列内の 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)) . これを行うためのより速い方法があるかどうか、誰かが知っていますか?もしそうなら、どのように?