303. Range Sum Query - Immutable
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 68% Topics: Array, Design, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# 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]
Solution from kamyu104/LeetCode-Solutions · MIT