oneby7

Range Queries Sparse table

606. Range Minimum Queries

Medium

You are given an array of integers nums and a list of queries, where queries[i] = [l, r].

The answer to a query is the smallest value among nums[l], nums[l + 1], …, nums[r]. Both ends are included, and positions count from 0. The array does not change between queries.

Return the answers in the same order as queries.

Example 1

Input: nums = [4, -2, 7, 3, -5, 6, 1] queries = [[0, 2], [2, 5], [3, 3], [0, 6], [5, 6]]

Output: [-2,-5,3,-5,1]

Explanation: [0,2] covers 4, -2 and 7, so its answer is -2. [2,5] covers 7, 3, -5 and 6. [3,3] holds only 3. The whole array [0,6] has -5 as its minimum, and [5,6] covers 6 and 1.

Example 2

Input: nums = [1000000000, -1000000000, 0] queries = [[0, 0], [0, 2], [2, 2]]

Output: [1000000000,-1000000000,0]

Explanation: A range of one position has that value as its minimum. The whole array holds -109.

Example 3

Input: nums = [8, 3, 3, 9] queries = [[0, 3], [0, 3], [3, 3]]

Output: [3,3,9]

Explanation: The same query can appear twice, and each copy gets its own answer. The answer is the smallest value, so it is 3 even though 3 appears twice.

Check your understanding

Work out the answer for this input before you write any code.

nums = [6, 2, 8, 5, 9], queries = [[2, 3]]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109
  • 1 ≤ queries.length ≤ 105
  • queries[i].length = 2
  • 0 ≤ l ≤ r < nums.length

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.