Advanced Coding Patterns for Interviews

0% completed

Previous
Next
Range Minimum Query (easy)

Problem Statement

You are given an array nums containing n integers and a 2D array queries of length q, where queries[i] = [start<sub>i</sub>, end<sub>i</sub>], representing a starting and ending range (inclusive).

Return an array of size q, where each element is the minimum value in the respective range from nums.

Examples

Example 1:

  • Input: nums = [2, 6, 1, 12, 9, 5, 3, 7], queries = [[0, 3], [2, 5], [0, 1], [3, 7], [0, 7], [4, 6], [4, 5]]
  • Output: [1, 1, 2, 3, 1, 3, 5]
  • Justification:
    • For the range [0, 3], the minimum is 1.
    • For the range [2, 5], the minimum is 1.
    • For the range [0, 1], the minimum is 2.
    • For the range [3, 7], the minimum is 3.
    • For the range [0, 7], the minimum is 1.
    • For the range [4, 6], the minimum is 3.
    • For the range [4, 5], the minimum is 5.

Example 2:

  • Input: nums = [4, 8, 6, 2, 10, 15], queries = [[1, 3], [0, 2], [2, 4], [3, 5]]
  • Output: [2, 4, 2, 2]
  • Justification:
    • For the range [1, 3], the minimum is 2.
    • For the range [0, 2], the minimum is 4.
    • For the range [2, 4], the minimum is 2.
    • For the range [3, 5], the minimum is 2.

Example 3:

  • Input: nums = [11, 13, 5, 8, 10, 12, 7, 14], queries = [[0, 4], [1, 3], [2, 5], [3, 6]]
  • Output: [5, 5, 5, 7]
  • Justification:
    • For the range [0, 4], the minimum is 5.
    • For the range [1, 3], the minimum is 5.
    • For the range [2, 5], the minimum is 5.
    • For the range [3, 6], the minimum is 7.

Constraints:

  • 0 <= start<sub>i</sub> < end<sub>i</sub> <= 1000

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next