0

I have a problem with my assignment which requires me to solve a problem that is similar to range-minimum-query. The problem is roughly described below:

I am supposed to code a java program which reads in large bunch of integers (about 100,000) and store them into some data structure. Then, my program must answer queries for the minimum number in a given range [i,j]. I have successfully devised an algorithm to solve this problem. However, it is just not fast enough.

The pseudo-code for my algorithm is as follows:

// Read all the integers into an ArrayList

// For each query,
// Read in range values [i,j] (note that i and j is "actual index" + 1 in this case)
// Push element at index i-1 into a Stack
// Loop from index i to j-1 in the ArrayList (tracking the current index with variable k)
[Begin loop]       
// If element at k is lesser than the one at the top of the stack, push the element at k into the Stack.
[End of loop]

Could someone please advise me on what I could do so that my algorithm would be fast enough to solve this problem?

The assignment files can be found at this link: http://bit.ly/1bTfFKa

I have been stumped by this problem for days. Any help would be much appreciated. Thanks.

Donald
  • 1,300
  • 2
  • 13
  • 29
  • 2
    Your problem sounds like **exactly** like the range minimum query, which there is sufficient material available for. See [this](http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor#Range_Minimum_Query_%28RMQ%29) for example. – Bernhard Barker Oct 15 '13 at 08:53
  • Hi. Thanks for the article. I found it to be quite helpful. Is it possible to use a stack based solution to solve this problem? I am wondering about this because the assignment brief actually states that rmq is not needed and that a Stack would be enough to solve the problem. – Donald Oct 15 '13 at 12:53
  • If you were told to use a stack, my guess is that you missed some detail of the problem description or described it incorrectly, because, as stated, it's the RMQ, which I don't believe there is an efficient stack-based solution for (that we know of). – Bernhard Barker Oct 15 '13 at 15:56

1 Answers1

0

Your problem is a static range minimum query (RMQ). Suppose you have N numbers. The simplest algorithm you could use is an algorithm that would create an array of size N and store the numbers, and another one that will be of size sqrtN, and will hold the RMQ of each interval of size sqrtN in the array. This should work since N is not very large, but if you have many queries you may want to use a different algorithm.
That being said, the fastest algorithm you could use is making a Sparse Table out of the numbers, which will allow you to answer the queries in O(1). Constructing the sparse table is O(NlogN) which, given N = 10^5 should be just fine.
Finally, the ultimate RMQ algorithm is using a Segment Tree, which also supports updates (single-element as well as ranges), and it's O(N) to construct the Segment Tree, and O(logN) per query and update. All of these algorithms are very well exposed here. For more information in Segment Trees see these tutorials I wrote myself. link
Good Luck!

  • Thanks for the useful links. They surely look interesting. Will try implementing a segment tree/sparse table solution when I have more free time. Anyway, I managed to solve the problem by pre-processing the input data of size N, into segments of size (cube root N). This was one of the RMQ methods stated in the link that Dukeling provided. – Donald Oct 16 '13 at 15:16
  • Well done, the best solution is not always the fastest one, or the most sophisticated one, but the one that solves a particular problem the simplest way possible. On a side note, I recommend learning Segment Trees because it's a very versatile data structure, adaptable to many range queries (like range sum queries, or even GCD queries) –  Oct 17 '13 at 14:15