Skip to content

Range Sum Query (Immutable)

EasyArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Build a class NumArray that is created from a list of integers nums and then answers many queries. Each query sum_range(left, right) (sumRange in TypeScript) returns the sum of nums[left] through nums[right], inclusive. The list never changes after construction.

arr = NumArray([-2, 0, 3, -5, 2, -1])
arr.sum_range(0, 2)   →  1    (-2 + 0 + 3)
arr.sum_range(2, 5)   →  -1   (3 - 5 + 2 - 1)
arr.sum_range(0, 5)   →  -3

Constraints

  • 1 <= nums.length <= 10^4
  • -10^5 <= nums[i] <= 10^5
  • 0 <= left <= right < nums.length
  • At most 10^4 calls to sum_range.