Segment tree · Medium
An incident report highlights the lowest observed reading in each selected period. It may ask about many overlapping periods in one fixed snapshot. The implementation must reuse an index rather than scanning all readings for every selection.
Return the minimum numeric reading for every requested inclusive period without changing the snapshot.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[], queries: [integer, integer][] }
Fields
- nums contains signed integer readings in observation order.
- queries is an array of inclusive zero-based [left,right] index pairs. Every pair satisfies 0 <= left <= right < nums.length.
- The values do not change between queries. If nums is empty, queries is empty. Neither input array may be modified.
Output
Return value
- Return minimum values, not positions, in query order. Equal minima do not require a tie-break.
- A single-position query returns that value. Empty queries return [].
Constraints
- 0 <= nums.length <= 100000; 0 <= queries.length <= 100000.
- All input indexes and counts are integers.
- -1000000000 <= nums[i] <= 1000000000.
Examples
Example 1
{
"nums": [
8,
-2,
5,
1
],
"queries": [
[
0,
0
],
[
1,
3
],
[
2,
3
]
]
}Returns
[
8,
-2,
1
]Example 2
{
"nums": [
6,
6
],
"queries": [
[
0,
1
]
]
}Returns
[
6
]Required complexity
O(n) index construction and O(log(n + 1)) per query with O(n) auxiliary space