All topics
147

Sparse table: static range minimum

Tier 1Locked

Where it runs

1. The problem

The last two lessons built segment trees. They answer a range question and take changes, both in O(log n). Now the data stops changing. A log file is closed at midnight, a reference table ships with the app, a puzzle grid is fixed in advance. There will be no more writes. And you still have to answer thousands of questions like "what is the smallest value between index l and r?"

A segment tree would work here, but O(log n) per query is more than you need to pay. If the array never changes, you can work out enough in advance that each range-minimum query costs O(1). This structure is called a sparse table.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included

On LeetCode

Problems on an array that never changes, with many minimum, maximum or GCD questions over ranges. After O(n log n) building, two overlapping blocks answer each range in O(1). LeetCode never names the sparse table, and a monotonic deque or a segment tree also passes most of these.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy