-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRMQ.java
More file actions
57 lines (50 loc) · 1.47 KB
/
Copy pathRMQ.java
File metadata and controls
57 lines (50 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class RMQ {
public int[][] queries;
public int[] pairs;
public RMQ(int[] array){
queries = new int[array.length][array.length];
pairs = array;
preProcess();
}
public void preProcess(){
for (int i = 0; i < pairs.length; i++){
for (int j = 0; j < pairs.length; j++){
queries[i][j] = getMinimum(i, j);
}
}
}
public int getMinimum(int i, int j){
int min = 0;
if (i < j) {
min = pairs[i];
for (int x = i + 1; x <= j; x++) {
if (min > pairs[x]) {
min = pairs[x];
}
}
}else if (i > j){
min = pairs[i];
for (int x = j + 1; x <= i; x++) {
if (min > pairs[x]) {
min = pairs[x];
}
}
}
return min;
}
public int getQuery(int i, int j){
if (i < queries.length && j < queries.length){
return queries[i][j];
}
return 0;
}
public static void main(String[] args) {
int[] ints = new int[10];
ints[0] = 1;
ints[1] = 5 ; ints[2] =9 ;ints[3] =-1; ints[4] = -12 ;ints[5] = 16 ;ints[6] = 7; ints[7] = 10; ints[8] = 25 ;ints[9] = 28;
RMQ rmq = new RMQ(ints);
System.out.println(rmq.getQuery(8,9));
System.out.println(rmq.getQuery(0,9));
System.out.println(rmq.getQuery(5,9));
}
}