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.