Range Sum Query - Immutable (Leetcode 303)

Problem Link: https://leetcode.com/problems/range-sum-query-immutable/description/

class NumArray:

    def __init__(self, nums: List[int]):

        self.prefixSum=[]
        summ=0
        for i in nums:
            summ=summ+i
            self.prefixSum.append(summ)
            
    def sumRange(self, left: int, right: int) -> int:

        if(left==0):
            return self.prefixSum[right]
        else:
            return self.prefixSum[right]-self.prefixSum[left-1]
        


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

Last updated