Skip to main content

307 - Range Sum Query - Mutable

Details

KeyValue
Linkhttps://leetcode.com/problems/range-sum-query-mutable/
LanguagePython 3
Runtime8841 ms, faster than 5.00% of Python3 online submissions for Range Sum Query - Mutable
Memory Usage31.2 MB, less than 66.27% of Python3 online submissions for Range Sum Query - Mutable
DatastructuresList[int]
AlgorithmsRange Sum Query
ComplexityTime: O(logN) Memory: O(1)

Procedure

  1. ...

Code

class NumArray:
def __init__(self, nums: List[int]):
self.nums = nums
self.sums = sum(nums)

def update(self, index: int, val: int) -> None:
self.sums += val - self.nums[index]
self.nums[index] = val

def sumRange(self, left: int, right: int) -> int:
if right-left < len(self.nums) // 2: return sum(self.nums[left:right+1])
return self.sums - sum(self.nums[:left]) - sum(self.nums[right+1:])

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