Range Sum Query - Immutable

Easy

Topics
ArrayPrefix SumDesign

Given an integer array nums, handle multiple queries of the sum of the elements between indices left and right inclusive. Implement NumArray(nums) and sumRange(left, right) which returns the sum nums[left] + ... + nums[right].

Example 1

Input:  operations = ["NumArray","sumRange","sumRange","sumRange"], values = [[-2,0,3,-5,2,-1],[0,2],[2,5],[0,5]]
Output: [null,1,-1,-3]

Constraints

  • 1 <= nums.length <= 10^4
  • -10^5 <= nums[i] <= 10^5
  • 0 <= left <= right < nums.length
  • At most 10^4 calls to sumRange.
Run ⌘' · Submit ⌘⏎