[1] https://leetcode.com/problems/minimum-height-trees/
OJ
[2] https://leetcode.com/discuss/71656/c-solution-o-n-time-o-n-space
308. Range Sum Query 2D - Mutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
Example:
Given matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5] ] sumRegion(2, 1, 4, 3) -> 8 update(3, 2, 2) sumRegion(2, 1, 4, 3) -> 10
Note:
//C++, Segment Tree //C++: 784ms class STNode{ public: int sum; int ss, se; STNode *left, *right; STNode(vector<int> &row, int s, int e){ sum = 0; ss = s; se = e; left = NULL; right = NULL; if(s==e){ sum = row[s]; }else{ int m = (s+e)/2; left = new STNode(row, s, m); right = new STNode(row, m+1, e); sum = left->sum+right->sum; } } }; class NumMatrix { int nRows; vector<STNode *> roots; void updateST(STNode* root, int j, int val){ if(!root || j<root->ss || j>root->se) return; else if(j==root->ss && j==root->se) root->sum = val; else{ updateST(root->left, j, val); updateST(root->right, j, val); root->sum = (root->left?root->left->sum:0) + (root->right?root->right->sum:0); } } int sumRange(STNode* root, int i, int j){ if(!root) return 0; else if(i>root->se || j<root->ss) return 0; else if(i<=root->ss && j>=root->se) return root->sum; else return sumRange(root->left, i, j)+sumRange(root->right, i, j); } public: NumMatrix(vector<vector<int>> &matrix) { nRows = matrix.size(); roots.resize(nRows); for(int i=0; i<nRows; ++i){ roots[i] = new STNode(matrix[i], 0, (int)matrix[i].size()-1); } } void update(int row, int col, int val) { updateST(roots[row], col, val); } int sumRegion(int row1, int col1, int row2, int col2) { int ret = 0; for(int i=row1; i<=row2; ++i){ ret += sumRange(roots[i], col1, col2); } return ret; } }; //Java, Segment Tree class SGTree{ int sum, start, end; SGTree left, right; SGTree(int[] nums, int l, int r){ start = l; end = r; if(start==end){ sum = nums[l]; }else{ int m = (l+r)/2; left = new SGTree(nums, l, m); right = new SGTree(nums, m+1, r); sum = left.sum + right.sum; } } }; class NumMatrix { int[] nums; SGTree root; int m, n; public NumMatrix(int[][] matrix) { m = matrix.length; if(m==0) return; n = matrix[0].length; if(n==0) return; nums = new int[m*n]; for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ nums[i*n+j] = matrix[i][j]; } } root = new SGTree(nums, 0, nums.length-1); } void updateST(SGTree node, int i, int v){ if(node==null || i<node.start || i>node.end) return; if(node.start==i && node.end==i) node.sum = v; else{ updateST(node.left, i, v); updateST(node.right, i, v); node.sum = node.left.sum+node.right.sum; } } int sumST(SGTree node, int l, int r){ if(node==null) return 0; if(r<node.start || l>node.end) return 0; if(l<=node.start && node.end<=r) return node.sum; int s = sumST(node.left, l, r) + sumST(node.right, l, r); return s; } public void update(int row, int col, int val) { updateST(root, row*n+col,val); } public int sumRegion(int row1, int col1, int row2, int col2) { int s = 0; for(int i=row1; i<=row2; ++i){ s += sumST(root, i*n+col1, i*n+col2); } return s; } }; /** * Your NumMatrix object will be instantiated and called as such: * NumMatrix obj = new NumMatrix(matrix); * obj.update(row,col,val); * int param_2 = obj.sumRegion(row1,col1,row2,col2); */ //Java, Binary Indexed Tree class NumMatrix { int[][] nums; int[][] tree; int m, n; public NumMatrix(int[][] matrix) { m = matrix.length; if(m==0) return; n = matrix[0].length; tree = new int[m+1][n+1]; nums = new int[m][n]; for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ update(i, j, matrix[i][j]); } } } public void update(int row, int col, int val) { int d = val-nums[row][col]; nums[row][col] = val; for(int i=row+1; i<=m; i+=i&(-i)){ for(int j=col+1; j<=n; j+=j&(-j)){ tree[i][j] += d; } } } //row and col are corrordinates for tree public int sum(int row, int col){ int s = 0; for(int i=row; i>0; i-=i&(-i)){ for(int j=col; j>0; j-=j&(-j)){ s += tree[i][j]; } } return s; } public int sumRegion(int row1, int col1, int row2, int col2) { return sum(row2+1, col2+1)-sum(row2+1,col1)-sum(row1, col2+1)+sum(row1, col1); } } /** * Your NumMatrix object will be instantiated and called as such: * NumMatrix obj = new NumMatrix(matrix); * obj.update(row,col,val); * int param_2 = obj.sumRegion(row1,col1,row2,col2); */
307. Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
The update(i, val) function modifies nums by updating the element at index i to val.
Example:
Given nums = [1, 3, 5] sumRange(0, 2) -> 9 update(1, 2) sumRange(0, 2) -> 8
Constraints:
0 <= i <= j <= nums.length - 1
//C++ //C++: 1720ms class STNode{ public: int sum; int ss; int se; STNode *left; STNode *right; STNode(vector<int> &nums, int s, int e){ sum = 0; ss = s; se = e; left = NULL; right = NULL; if(s==e){ sum = nums[s]; }else if(s<e){ int m = (s+e)/2; left = new STNode(nums, s, m); right = new STNode(nums, m+1, e); sum = left->sum + right->sum; } } }; class NumArray { STNode * root; void updateST(STNode* root, int i, int val){ if(!root || i<root->ss || i>root->se){ return; }if(i==root->ss && i==root->se){ root->sum = val; }else{ updateST(root->left, i, val); updateST(root->right, i, val); root->sum = (root->left?root->left->sum:0) + (root->right?root->right->sum:0); } } int sumRange(STNode* root, int i, int j){ if(!root) return 0; else if(i>root->se || j<root->ss) return 0; else if(i<=root->ss && j>=root->se) return root->sum; else return sumRange(root->left, i, j)+sumRange(root->right, i, j); } public: NumArray(vector<int> &nums) { int n = nums.size(); root = new STNode(nums, 0, n-1); } void update(int i, int val) { updateST(root, i, val); } int sumRange(int i, int j) { return sumRange(root, i, j); } }; //Java class SGTree{ int sum; int start; int end; SGTree left; SGTree right; SGTree(int[] nums, int l, int r){ start = l; end = r; if(start==end){ sum = nums[l]; }else{ int m = (l+r)/2; left = new SGTree(nums, l, m); right = new SGTree(nums, m+1, r); sum = left.sum + right.sum; } } }; public class NumArray { SGTree sgTree; public NumArray(int[] nums) { if(nums.length>0) sgTree = new SGTree(nums, 0, nums.length-1); } void updateST(SGTree node, int i, int v){ if(node==null || i<node.start || i>node.end) return; if(i==node.start && i==node.end) node.sum = v; else{ updateST(node.left, i, v); updateST(node.right, i, v); node.sum = node.left.sum + node.right.sum; } } int sumST(SGTree node, int i, int j){ if(node==null || j<node.start || i>node.end) return 0; if(i<=node.start && node.end<=j) return node.sum; return sumST(node.left, i, j)+sumST(node.right, i, j); } public void update(int i, int val) { updateST(sgTree, i, val); } public int sumRange(int i, int j) { return sumST(sgTree, i, j); } } /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * obj.update(i,val); * int param_2 = obj.sumRange(i,j); */
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 | class NumArray { int n; int[] bit; int[] arr; int getNext(int i){ return i+(i&(-i)); } int getParent(int i){ return i-(i&(-i)); } public NumArray(int[] nums) { n = nums.length; bit = new int[n+1]; arr = new int[n]; for(int i=0; i<n; ++i){ update(i, nums[i]); } } public void update(int index, int val) { int diff = val-arr[index]; arr[index] = val; int p = index+1; while(p<=n){ bit[p] += diff; p = getNext(p); } } int getPreSum(int index){ int sum = 0; int p = index+1; while(p>0){ sum += bit[p]; p = getParent(p); } return sum; } public int sumRange(int left, int right) { return getPreSum(right)-getPreSum(left-1); } } /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * obj.update(index,val); * int param_2 = obj.sumRange(left,right); */ |
304. Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
Example:
Given matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5] ] sumRegion(2, 1, 4, 3) -> 8 sumRegion(1, 1, 2, 2) -> 11 sumRegion(1, 2, 2, 4) -> 12
Note:
//C++: class NumMatrix { vector<vector<int>> mt; public: NumMatrix(vector<vector<int>> &matrix) { int m = matrix.size(); if(m==0) return; int n = matrix[0].size(); if(n==0) return; mt.resize(m+1, vector<int>(n+1, 0)); for(int i=1; i<=m; ++i){ for(int j=1; j<=n; ++j){ mt[i][j] += matrix[i-1][j-1] + mt[i-1][j] + mt[i][j-1] - mt[i-1][j-1]; } } } int sumRegion(int row1, int col1, int row2, int col2) { return mt[row2+1][col2+1] - mt[row2+1][col1] - mt[row1][col2+1] + mt[row1][col1]; } }; //Java class NumMatrix { int[][] extMatrix; public NumMatrix(int[][] matrix) { int m = matrix.length; if(m==0) return; int n = matrix[0].length; if(n==0) return; extMatrix = new int[m+1][n+1]; for(int i=1; i<=m; ++i){ for(int j=1; j<=n; ++j){ extMatrix[i][j] = extMatrix[i-1][j]+extMatrix[i][j-1]+matrix[i-1][j-1]-extMatrix[i-1][j-1]; } } } public int sumRegion(int row1, int col1, int row2, int col2) { int r = extMatrix[row2+1][col2+1]-extMatrix[row2+1][col1]-extMatrix[row1][col2+1]+extMatrix[row1][col1]; return r; } } /** * Your NumMatrix object will be instantiated and called as such: * NumMatrix obj = new NumMatrix(matrix); * int param_1 = obj.sumRegion(row1,col1,row2,col2); */
//Java, BIT class NumArray { int[] arr; int[] tree; int m; public NumArray(int[] nums) { m = nums.length; arr = new int[m]; tree = new int[m+1]; for(int i=0; i<m; ++i){ update(i, nums[i]); } } //index for arr public void update(int k, int val) { int d = val-arr[k]; arr[k] = val; for(int i=k+1; i<=m; i+=i&(-i)){ tree[i]+=d; } } //index for tree public int sum(int k){ int s = 0; for(int i=k; i>0; i-=i&(-i)){ s += tree[i]; } return s; } public int sumRange(int i, int j) { return sum(j+1)-sum(i); } } /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * obj.update(i,val); * int param_2 = obj.sumRange(i,j); */
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3
Constraints:
0 <= nums.length <= 10^4
-10^5 <= nums[i] <= 10^5
0 <= i <= j < nums.length
//C++ class NumArray { vector<int> sum; public: NumArray(vector<int> &nums) { for(auto n:nums){ sum.push_back(n+(sum.empty()?0:sum.back())); } } int sumRange(int i, int j) { return sum[j]-(i==0?0:sum[i-1]); } }; // Your NumArray object will be instantiated and called as such: // NumArray numArray(nums); // numArray.sumRange(0, 1); // numArray.sumRange(1, 2); //Java class NumArray { int[] nums; public NumArray(int[] nums) { int s = 0; this.nums = Arrays.copyOf(nums, nums.length); for(int i=0; i<nums.length; ++i){ s += nums[i]; this.nums[i] = s; } } public int sumRange(int i, int j) { return this.nums[j] - (i-1>=0?this.nums[i-1]:0); } } /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * int param_1 = obj.sumRange(i,j); */