forked from Kyrylo-Ktl/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange Sum Query - Mutable.py
More file actions
57 lines (44 loc) · 1.33 KB
/
Range Sum Query - Mutable.py
File metadata and controls
57 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import List
class BITTree:
"""
Implementation of Binary Indexed Tree/Fenwick Tree
Memory:
creation - O(n)
update - O(1)
get_sum - O(1)
Time:
creation - O(n*log(n))
update - O(log(n))
get_sum - O(log(n))
"""
def __init__(self, nums: List[int]):
self.bi_tree = [0] * (len(nums) + 1)
self.n = len(nums)
for i in range(self.n):
self.update(i + 1, nums[i])
def update(self, index: int, value: int):
while index <= self.n:
self.bi_tree[index] += value
index += self.low_bit(index)
def get_sum(self, index: int) -> int:
prefix = 0
while index > 0:
prefix += self.bi_tree[index]
index -= self.low_bit(index)
return prefix
@staticmethod
def low_bit(bit: int) -> int:
return bit & -bit
class NumArray:
"""
Time: O(n*log(n)) + O(log(n))
Memory: O(n)
"""
def __init__(self, nums: List[int]):
self.nums = nums
self.bi_tree = BITTree(nums)
def update(self, i: int, val: int):
self.bi_tree.update(i + 1, val - self.nums[i])
self.nums[i] = val
def sumRange(self, left: int, right: int) -> int:
return self.bi_tree.get_sum(right + 1) - self.bi_tree.get_sum(left)