I am a computer science engineering student and I came to this problem in many tasks.
I am given an array of size with values
and then asked a number of queries
.
In each query I am given two indices where
.
I believe, there is a way to answer this queries in if some pre-processing is done.
I have to do it in .
I have been on this problem for many days, but I can not get it at all.
This problem is known as "range minimum query". It is well-studied, and if you Google that, you will end up at the well-known, very clever, and pretty complicated data structure that meets your requirements, requiring O(N) space and preprocessing time.
Here I'll provide a much simpler solution that performs better in practice whenever your array contains less than 264 items... i.e., always.
I'll only talk about finding the minimum, but of course finding the maximum requires a similar structure.
Like the well-known data structure, it uses this one as a component:
Given an array A of N items, let M = ceil(log2 N).
Make a matrix W of size NxM and, for each (i,j) with 0 < i < N and 1 < j < M, assign W[i][j] to the index of the minimum value amongst the 2j elements starting at A[i], stopping at the end of the array.
So now, for every window of size 2j, we have the index of its minimum value. Since every subarray is exactly covered by at most 2 overlapping windows, we can easily find the minimum value in any subarray by checking those 2 windows and picking the lower of their 2 minimums.
Initializing this data structure can be done in constant time per item. If you do the smaller window sizes first, then each window minimum can be found by merging the results for 2 smaller windows.
This solution requires storing log N indexes per array item.
To make a solution fit into O(N) space, we need to use the preceding data structure at 2 levels:
Now, we've spent only 8 or 12 bytes per item (depending on whether array indexes are 32 or 64 bits), and we can easily answer any range-minimum query in constant time:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With