-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRange Sum Query - Mutable.js
More file actions
43 lines (36 loc) · 993 Bytes
/
Range Sum Query - Mutable.js
File metadata and controls
43 lines (36 loc) · 993 Bytes
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
var NumArray = function(nums) {
this.n = nums.length;
this.tree = new Array(2 * this.n).fill(0);
for (let i = 0; i < this.n; i++) {
this.tree[this.n + i] = nums[i];
}
for (let i = this.n - 1; i > 0; i--) {
this.tree[i] = this.tree[2 * i] + this.tree[2 * i + 1];
}
};
NumArray.prototype.update = function(index, val) {
let pos = index + this.n;
this.tree[pos] = val;
while (pos > 1) {
pos = Math.floor(pos / 2);
this.tree[pos] = this.tree[2 * pos] + this.tree[2 * pos + 1];
}
};
NumArray.prototype.sumRange = function(left, right) {
let sum = 0;
left += this.n;
right += this.n;
while (left <= right) {
if (left % 2 === 1) {
sum += this.tree[left];
left++;
}
if (right % 2 === 0) {
sum += this.tree[right];
right--;
}
left = Math.floor(left / 2);
right = Math.floor(right / 2);
}
return sum;
};