range sum query mutable

🏠
 1 class NumArray:
 2 
 3     def __init__(self, nums: List[int]):
 4         self.sums = [0]*len(nums)
 5         self.nums = nums
 6         s = 0
 7         for i, v in enumerate(nums):
 8             s += v
 9             self.sums[i] = s
10 
11     def update(self, i: int, val: int) -> None:
12         d = val - self.nums[i]
13         self.nums[i] = val
14         for i, v in enumerate(self.sums[i:], i):
15             self.sums[i] += d
16 
17     def sumRange(self, i: int, j: int) -> int:
18         return self.sums[j] - (self.sums[i-1] if i - 1 >= 0 else 0)