Apex › Storage & databases

Sparse table — O(1) range minimum

Answer "what's the minimum in [l, r]?" in constant time for a static array, after an O(n log n) precompute. Because min is idempotent, any range is covered by two overlapping power-of-two blocks and the double-counted middle does no harm — so a query is one min of two lookups, no loop. Drag the range and watch the two blocks snap into place.

More in Storage & databases