Possible Duplicate:
Find the min number in all contiguous subarrays of size l of a array of size n
I have a (large) array of numeric data (size N
) and would like to compute an array of running maximums with a fixed window size w
.
More directly, I can define a new array out[k-w+1] = max{data[k-w+1,...,k]}
for k >= w-1
(this assumes 0-based arrays, as in C++).
Is there a better way to do this than N log(w)
?
[I'm hoping there should be a linear one in N
without dependence on w
, like for moving average, but cannot find it. For N log(w)
I think there is a way to manage with a sorted data structure which will do insert()
, delete()
and extract_max()
altogether in log(w)
or less on a structure of size w
-- like a sorted binary tree, for example].
Thank you very much.
I'll demonstrate how to do it with the list:
with length
N=23
andW = 4
.Make two new copies of your list:
Loop from
i=0
toN-1
. Ifi
is not divisible byW
, then replaceL1[i]
withmax(L1[i],L1[i-1])
.Loop from
i=N-2
to0
. Ifi+1
is not divisible byW
, then replaceL2[i]
withmax(L2[i], L2[i+1])
.Make a list
L3
of lengthN + 1 - W
, so thatL3[i] = max(L2[i]
,L1[i + W - 1])
Then this list
L3
is the moving maxima you seek,L2[i]
is the maximum of the range betweeni
and the next vertical line, whilel1[i + W - 1]
is the maximum of the range between the vertical line andi + W - 1
.There is indeed an algorithm that can do this in O(N) time with no dependence on the window size w. The idea is to use a clever data structure that supports the following operations:
This is essentially a queue data structure that supports access (but not removal) of the maximum element. Amazingly, as seen in this earlier question, it is possible to implement this data structure such that each of these operations runs in amortized O(1) time. As a result, if you use this structure to enqueue w elements, then continuously dequeue and enqueue another element into the structure while calling find-max as needed, it will take only O(n + Q) time, where Q is the number of queries you make. If you only care about the minimum of each window once, this ends up being O(n), with no dependence on the window size.
Hope this helps!