Range Sum Queries
Easy · prefix sum
You are given a list of integers nums and a list of queries. Each query is a pair [left, right] that names a stretch of nums from index left to index right, both ends included.
For every query, work out the total of the numbers in that stretch. Return a list with one total per query, in the same order as the queries. The list nums never changes between queries.
Examples
Example 1
Input: nums = [3, -2, 5, 1, 4], queries = [[0, 2], [1, 3], [4, 4]]
Output: [6, 4, 4]
3 + (-2) + 5 = 6, then (-2) + 5 + 1 = 4, and the last query is the single number 4.
Example 2
Input: nums = [10, 20, 30], queries = [[0, 2], [0, 0]]
Output: [60, 10]
Example 3
Input: nums = [-1, -1, -1, -1], queries = [[1, 2], [0, 3]]
Output: [-2, -4]
Constraints
- 1 <= len(nums) <= 5000
- -10^5 <= nums[i] <= 10^5
- 1 <= len(queries) <= 5000
- queries[i] = [left, right] with 0 <= left <= right < len(nums)