53. Totals by Range
Easy
An inventory spreadsheet records the change in units at each position of an array. To answer multiple queries without repeatedly scanning the same ranges, you are given the array nums and a list of consultas.
Each query is a pair [i, j], with indices starting at zero. Consider the positions from i through j, inclusive, and return the sum of the values in that range. Return an array with one answer per query, in the same order as they appear in consultas.
Example 1
Input: nums = [4, -2, 7, 1], consultas = [[0, 2], [1, 3], [2, 2]]
Output: [9, 6, 7]
Explanation: Each answer sums the values between the indicated indices, including both endpoints.
Example 2
Input: nums = [5], consultas = [[0, 0]]
Output: [5]
Example 3
Input: nums = [3, 3, 3], consultas = []
Output: []
Constraints
- 1 <= nums.length <= 100000
- -10000 <= nums[i] <= 10000
- 0 <= consultas.length <= 100000
- Each query contains exactly two indices [i, j].
- 0 <= i <= j < nums.length
Prefix sumsArrays
Saved in this browser
Caso 1
nums =
[4,-2,7,1]
consultas =
[[0,2],[1,3],[2,2]]
Expected =
[9,6,7]
Caso 2
nums =
[5]
consultas =
[[0,0]]
Expected =
[5]
Caso 3
nums =
[3,3,3]
consultas =
[]
Expected =
[]
Run checks these 3 cases. Submit runs all 11, including hidden ones.