992. Static Range Max

Medium3.5 points estimatedArrayDivide and Conquer3 hints

A weather station stores one wind-speed reading per hour in the array nums, with hours numbered from 0. Analysts then ask many questions about a fixed history: each query queries[j] = [l, r] asks for the highest reading among hours l through r, both included.

Return an array containing the answer to every query, in the order the queries are given. The readings are never modified between queries, and they may be negative (for example, signed gust deviation values). A query always has l <= r, so the range is never empty.

Example 1

Input:
nums = [5,1,9,3,7,2]queries = [[0,1],[1,3],[2,5],[4,4],[0,5]]
Output:
[5,9,9,7,9]
Explanation:

The maxima of the ranges are max(5,1) = 5, max(1,9,3) = 9, max(9,3,7,2) = 9, the single value 7, and 9 for the whole array.

Example 2

Input:
nums = [-4,-9,-1], queries = [[0,1],[1,2]]
Output:
[-4,-1]
Explanation:

All values are negative: the larger of -4 and -9 is -4, and the larger of -9 and -1 is -1.

Constraints

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

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(n log n + q)
Space
O(n log n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…