You are given an integer array nums, handle multiple queries of the following type:
nums between indices left and right inclusive where left <= right.Implement the NumArray class:
NumArray(int[] nums) Initializes the object with the integer array nums.
int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).
Example 1:
Input: ["NumArray","sumRange","sumRange","sumRange"]
[[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]
Output: [null,1,-1,-3]Explanation: NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
Constraints:
1 <= nums.length <= 10,000-100,000 <= nums[i] <= 100,0000 <= left <= right < nums.lengthsumRange.Before attempting this problem, you should be comfortable with:
The simplest approach is to iterate through the array from left to right and add up all the elements. This works correctly but is inefficient when we need to answer many queries, since each query requires scanning through the entire range.
sumRange(left, right) query:res = 0.left to right.res.res.We can precompute a prefix sum array where prefix[i] stores the sum of all elements from index 0 to i. To find the sum of any range [left, right], we take prefix[right] and subtract prefix[left - 1] (if left > 0). This gives us constant-time queries after a linear-time preprocessing step.
cur.cur and store in prefix.sumRange(left, right) query:rightSum = prefix[right].left > 0, set leftSum = prefix[left - 1], otherwise leftSum = 0.rightSum - leftSum.This is a cleaner variation of the prefix sum approach. By creating an array of size n + 1 where prefix[0] = 0, we avoid the edge case when left = 0. The value prefix[i + 1] represents the sum of the first i + 1 elements. The range sum becomes simply prefix[right + 1] - prefix[left].
prefix array of size n + 1 initialized to 0.i, set prefix[i + 1] = prefix[i] + nums[i].sumRange(left, right) query:prefix[right + 1] - prefix[left].A segment tree is a binary tree where each node stores the sum of a range of elements. The root contains the sum of the entire array, and each leaf contains a single element. While this is overkill for an immutable array (prefix sums are simpler and faster), segment trees become essential when updates are needed. For this immutable version, queries still run in logarithmic time.
n to 2n - 1.tree[i] = tree[2*i] + tree[2*i + 1].sumRange(left, right) query:left and right to leaf positions.left < right:left is odd, add tree[left] to the result and increment left.right is odd, decrement right and add tree[right] to the result.class SegmentTree:
def __init__(self, nums):
self.n = len(nums)
self.tree = [0] * (2 * self.n)
for i in range(self.n):
self.tree[self.n + i] = nums[i]
for i in range(self.n - 1, 0, -1):
self.tree[i] = self.tree[2 * i] + self.tree[2 * i + 1]
def query(self, left, right):
left += self.n
right += self.n + 1
res = 0
while left < right:
if left % 2 == 1:
res += self.tree[left]
left += 1
if right % 2 == 1:
right -= 1
res += self.tree[right]
left //= 2
right //= 2
return res
class NumArray:
def __init__(self, nums):
self.segTree = SegmentTree(nums)
def sumRange(self, left, right):
return self.segTree.query(left, right)When using a prefix sum array where prefix[i] represents the sum from index 0 to i, computing sumRange(0, right) requires special handling. Attempting to access prefix[-1] causes an error. The fix is either to check if left > 0 before subtracting, or use a prefix array of size n + 1 where prefix[0] = 0 so that prefix[right + 1] - prefix[left] always works without edge cases.
Since prefix sums are precomputed during initialization, any subsequent changes to the original array will not be reflected in query results. This approach only works for immutable arrays. If updates are needed, a different data structure like a segment tree or Fenwick tree must be used instead of static prefix sums.