-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathRangeSumMutable.java
86 lines (76 loc) · 2.59 KB
/
RangeSumMutable.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class NumArray {
class SegmentTreeNode {
private int start, end;
private SegmentTreeNode left, right;
private int sum;
public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
this.left = null;
this.right = null;
this.sum = 0;
}
}
SegmentTreeNode root = null;
public NumArray(int[] nums) {
root = buildTree(nums, 0, nums.length-1);
}
// TC : O(n)
private SegmentTreeNode buildTree(int[] nums, int start, int end) {
if (start > end) {
return null;
} else {
SegmentTreeNode ret = new SegmentTreeNode(start, end);
if (start == end) {
ret.sum = nums[start]; // leaf nodes
} else {
int mid = start + (end - start) / 2;
ret.left = buildTree(nums, start, mid);
ret.right = buildTree(nums, mid + 1, end);
ret.sum = ret.left.sum + ret.right.sum;
}
return ret;
}
}
// TC : O(logn)
void update(int i, int val) {
updateHelper(root, i, val);
}
void updateHelper(SegmentTreeNode root, int pos, int val) {
// found leaf node to be updated
if (root.start == root.end) {
root.sum = val;
} else {
// parent nodes across the path
int mid = root.start + (root.end - root.start) / 2;
if (pos <= mid) {
updateHelper(root.left, pos, val);
} else {
updateHelper(root.right, pos, val);
}
root.sum = root.left.sum + root.right.sum;
}
}
public int sumRange(int i, int j) {
return sumRangeHelper(root, i, j);
}
// TC : O(logn)
public int sumRangeHelper(SegmentTreeNode root, int start, int end) {
// if you found out the node that matches your search return its value
if (root.start == start && root.end == end ) {
return root.sum;
} else {
int mid = root.start + (root.end - root.start) / 2; // overflow conditions
if (end <= mid) {
// move left
return sumRangeHelper(root.left, start, end);
} else if (start >= mid+1) {
// move right
return sumRangeHelper(root.right, start, end);
} else {
// consider both nodes
return sumRangeHelper(root.left, start, mid) + sumRangeHelper(root.right, mid+1, end) ;
}
}
}
}