Skip to content

Commit 152dfc0

Browse files
committed
Sparse table slides
1 parent ff52e0e commit 152dfc0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
Binary file not shown.

src/main/java/com/williamfiset/algorithms/datastructures/sparsetable/SparseTable.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*
55
* <p>Main inspiration: https://cp-algorithms.com/data_structures/sparse-table.html
66
*
7+
* <p>Tested against: https://www.spoj.com/problems/RMQSQ
8+
*
79
* <p>To run this file:
810
*
911
* <p>./gradlew run -Pmain=com.williamfiset.algorithms.datastructures.sparsetable.SparseTable
@@ -168,6 +170,7 @@ private long sumQuery(int l, int r) {
168170
for (int p = P; p >= 0; p--) {
169171
int rangeLength = r - l + 1;
170172
if ((1 << p) <= rangeLength) {
173+
System.out.printf("[%d, %d)\n", l, l + (1<<p));
171174
sum += dp[p][l];
172175
l += (1 << p);
173176
}
@@ -191,16 +194,50 @@ private long query(int l, int r, BinaryOperator<Long> fn) {
191194
/* Example usage: */
192195

193196
public static void main(String[] args) {
197+
example3();
198+
}
199+
200+
private static void example1() {
194201
long[] values = {2, -3, 4, 1, 0, -1, -1, 5, 6};
195202

196203
// Initialize sparse table to do range minimum queries.
197-
SparseTable sparseTable = new SparseTable(values, Operation.MIN);
204+
SparseTable sparseTable = new SparseTable(values, SparseTable.Operation.MIN);
198205

199206
// Prints: "Min value between [2, 7] = -1"
200207
System.out.printf("Min value between [2, 7] = %d\n", sparseTable.query(2, 7));
201208

202209
// Prints: "Index of min value between [2, 7] = 5". Returns the leftmost index in the
203210
// event that there are duplicates.
204-
System.out.printf("Index of min value between [2, 7] = %d\n", sparseTable.queryIndex(2, 7));
211+
System.out.printf("Index of min value between [2, 7] = %d\n", sparseTable.queryIndex(2, 7));
212+
}
213+
214+
private static void example2() {
215+
long[] values = {4, 2, 3, 7, 1, 5, 3, 3, 9, 6, 7, -1, 4};
216+
System.out.println(values.length);
217+
218+
// Initialize sparse table to do range minimum queries.
219+
SparseTable sparseTable = new SparseTable(values, SparseTable.Operation.MIN);
220+
221+
// Prints: "Min value between [2, 7] = -1"
222+
System.out.printf("Min value between [2, 7] = %d\n", sparseTable.query(2, 7));
223+
224+
// Prints: "Index of min value between [2, 7] = 5". Returns the leftmost index in the
225+
// event that there are duplicates.
226+
System.out.printf("Index of min value between [2, 7] = %d\n", sparseTable.queryIndex(2, 7));
227+
}
228+
229+
private static void example3() {
230+
long[] values = {4, 2, 3, 7, 1, 5, 3, 3, 9, 6, 7, -1, 4,1,1,1,1,2,1111,1,1};
231+
System.out.println(values[17]);
232+
233+
// Initialize sparse table to do range minimum queries.
234+
SparseTable sparseTable = new SparseTable(values, SparseTable.Operation.SUM);
235+
236+
// Prints: "Min value between [2, 7] = -1"
237+
System.out.printf("Min value between [5, 17] = %d\n", sparseTable.query(5, 17));
238+
239+
// Prints: "Index of min value between [5, 17] = 5". Returns the leftmost index in the
240+
// event that there are duplicates.
241+
// System.out.printf("Index of min value between [5, 17] = %d\n", sparseTable.queryIndex(2, 7));
205242
}
206243
}

0 commit comments

Comments
 (0)