supercharger's blog

By supercharger, 9 years ago, In English

Can we tweak RMQ for finding Top 2 or 3 Elements with in a single range ( like using another Data structure to augment) .

A Naive RMQ algorithm looks like below,

//M[][][0] contains the top min element, and M[][][1] is the second min
private void computeRMQ(int M[][][], int A[], int N) {
		for (int i = 0; i < A.length; i++) {
			M[i][i][0] = i;
			M[i][i][1] = i;
		}
		for (int i = 0; i < A.length; i++) {
			for (int j = i + 1; j < A.length; j++) {		
				if (A[M[i][j - 1][0]] < A[j]) {
					M[i][j][1] = M[i][j][0];
                                        M[i][j][0] = M[i][j - 1][0];
				} else {
                                        M[i][j][1] = M[i][j][0];
					M[i][j][0] = j;
				}
                                //some cases ignored
			}
		}
	}

But, for the Can you help with any pointers for the standard O(N), O(logN) implentation....

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it