Easy

Range Sum Query - ImmutablePython

Full explanation · Time ctor: O(n), lookup: O(1) · Space O(n)

# Time:  ctor:   O(n),
#        lookup: O(1)
# Space: O(n)

class NumArray(object):
    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.accu = [0]
        for num in nums:
            self.accu.append(self.accu[-1] + num),

    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        """
        return self.accu[j + 1] - self.accu[i]