The problem
Return a list with the smallest value of a[l:r] for each query (l, r). The list a never changes, and every range has at least one value.
range_min([5, 2, 7, 4, 6, 1, 8, 3],
[(1, 7), (2, 5), (0, 1), (6, 8)])
# [1, 4, 5, 3]
Every range is half-open, as in Python slices: (l, r) means positions l up to but not including r, the same values as a[l:r].
One test passes in numbers that count how often they are compared with <, <=, > or >= (min and max compare too). They behave like ordinary numbers otherwise. This is how the tests tell the method apart from slower ones, which a timer cannot always do.
Limits
- Up to 100,000 values and 100,000 queries. Build in O(n log n), then answer each query with O(1) comparisons: at most n log₂ n + 2q comparisons in all for q queries.
Builds on RNGQ-2 Sparse table.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | Length one | Not run | |
| 3 | Lengths that are not powers of two | Not run | |
| 4 | Repeated and negative | Not run | |
| 5 | One value | Not run | |
| 6 | Comparisons | Not run | |
| 7 | 100,000 values | Not run |