RNGQ-P2

The smallest in a range

Practice 2 of 6Range queries7 tests

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

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

7 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2Length oneNot run
3Lengths that are not powers of twoNot run
4Repeated and negativeNot run
5One valueNot run
6ComparisonsNot run
7100,000 valuesNot run