File tree 2 files changed +81
-0
lines changed
2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Your NumArray object will be instantiated and called as such:
3
+ * NumArray obj = new NumArray(nums);
4
+ * obj.update(i,val);
5
+ * int param_2 = obj.sumRange(i,j);
6
+ *
7
+ * Runtime : 11ms
8
+ * Memory : 45.4MB
9
+ * Logic : use segment tree for finding out the sum of the array at given range.
10
+ */
11
+
12
+
13
+ class NumArray {
14
+
15
+ int [] tree ;
16
+ int n ;
17
+
18
+ public NumArray (int [] nums ) {
19
+ if (nums .length > 0 ) {
20
+ n = nums .length ;
21
+ tree = new int [n * 2 ];
22
+ buildTree (nums );
23
+ }
24
+ }
25
+
26
+ // build the segment tree and store it in array of size 2 * nums[] length
27
+ // so the nums[] values are stores at leaves of the segment tree.
28
+ private void buildTree (int [] nums ) {
29
+ for (int i = n , j = 0 ; i < 2 * n ; i ++, j ++)
30
+ tree [i ] = nums [j ];
31
+ for (int i = n - 1 ; i > 0 ; --i )
32
+ tree [i ] = tree [i * 2 ] + tree [i * 2 + 1 ];
33
+ }
34
+
35
+ // update the segment tree if new value to be inserted in tree, then update its parent at pos/2 and so on till root
36
+ void update (int pos , int val ) {
37
+ pos += n ;
38
+ tree [pos ] = val ;
39
+ while (pos > 0 ) {
40
+ int left = pos ;
41
+ int right = pos ;
42
+ if (pos % 2 == 0 ) {
43
+ right = pos + 1 ;
44
+ } else {
45
+ left = pos - 1 ;
46
+ }
47
+ // parent is updated after child is updated
48
+ tree [pos / 2 ] = tree [left ] + tree [right ];
49
+ pos /= 2 ;
50
+ }
51
+ }
52
+
53
+
54
+ // this is responsible for the sum betweeen given ranges
55
+ public int sumRange (int l , int r ) {
56
+ // get leaf with value 'l'
57
+ l += n ;
58
+ // get leaf with value 'r'
59
+ r += n ;
60
+ int sum = 0 ;
61
+ while (l <= r ) {
62
+ if ((l % 2 ) == 1 ) {
63
+ sum += tree [l ];
64
+ l ++;
65
+ }
66
+ if ((r % 2 ) == 0 ) {
67
+ sum += tree [r ];
68
+ r --;
69
+ }
70
+ l /= 2 ;
71
+ r /= 2 ;
72
+ }
73
+ return sum ;
74
+ }
75
+
76
+
77
+ }
78
+
79
+
Original file line number Diff line number Diff line change @@ -133,6 +133,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
133
133
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
134
134
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
135
135
| 103 | [ ZigZag Level Order] ( https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ ) | [ JavaScript] ( ./JavaScript/Binary-Tree-ZigZag-Traversal.js ) | _ O(n)_ | _ O(n)_ | Medium | Binary Tree | |
136
+ | 307 | [ Range Sum Query - Mutable] ( https://leetcode.com/problems/range-sum-query-mutable/ ) | [ Java] ( ./Java/Range-Sum-Query-Mutable.java ) | _ O(logn)_ | _ O(n)_ | Medium | Segment Tree | |
137
+
136
138
137
139
<br />
138
140
<div align =" right " >
You can’t perform that action at this time.
0 commit comments