Problem of The Day: Range Sum Query - Immutable
Problem Statement
My Solution
class NumArray:
def __init__(self, nums: List[int]):
self.prefix = []
self.nums = nums[:]
curr = 0
for num in nums:
curr += num
self.prefix.append(curr)
def sumRange(self, left: int, right: int) -> int:
return self.prefix[right] - self.prefix[left] + self.nums[left]
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(left,right)