303. Range Sum Query - Immutable

# Easy

Two methods:

  1. Brute Force

  2. Cache: while implementing init(), compute the sum of [0:i] by the way, sumRange[i, j] = sum[j+1]-sum[i]

class NumArray:

    def __init__(self, nums: List[int]):
        self.sum = [0 for i in range(len(nums)+1)]
        for i in range(len(nums)):
            self.sum[i+1] = self.sum[i] + nums[i] 
        

    def sumRange(self, i: int, j: int) -> int:
        return self.sum[j+1] - self.sum[i]


# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)

Last updated