首页 > 其他 > 详细

Leetcode: Range Sum Query 2D - Mutable

时间:2016-01-18 06:53:56      阅读:204      评论:0      收藏:0      [点我收藏+]
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).

Range Sum Query 2D
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:
The matrix is only modifiable by the update function.
You may assume the number of calls to update and sumRegion function is distributed evenly.
You may assume that row1 ≤ row2 and col1 ≤ col2.

参考:https://leetcode.com/discuss/72685/share-my-java-2-d-binary-indexed-tree-solution

Initializing the binary indexed tree takes O(mn*logm*logn) time, both update() and getSum()take O(logm*logn) time. The arr[][] is used to keep a backup of the matrix[][] so that we know the difference of the updated element and use that to update the binary indexed tree. The idea of calculating sumRegion() is the same as in Range Sum Query 2D - Immutable.

Binary Index Tree参见:https://www.youtube.com/watch?v=CWDQJGaN1gY

 1 public class NumMatrix {
 2   int m, n;
 3   int[][] arr;    // stores matrix[][]
 4   int[][] BITree; // 2-D binary indexed tree
 5 
 6   public NumMatrix(int[][] matrix) {
 7     if (matrix.length == 0 || matrix[0].length == 0) {
 8         return;
 9     }
10 
11     m = matrix.length;
12     n = matrix[0].length;
13 
14     arr = new int[m][n];
15     BITree = new int[m + 1][n + 1];
16 
17     for (int i = 0; i < m; i++) {
18       for (int j = 0; j < n; j++) {
19         update(i, j, matrix[i][j]); // init BITree[][]
20         arr[i][j] = matrix[i][j];   // init arr[][]
21       }
22     }
23   }
24 
25   public void update(int i, int j, int val) {
26     int diff = val - arr[i][j];  // get the diff
27     arr[i][j] = val;             // update arr[][]
28 
29     i++; j++;
30     while (i <= m) {
31       int k = j;
32       while (k <= n) {
33         BITree[i][k] += diff; // update BITree[][]
34         k += k & (-k); // update column index to that of parent
35       }
36       i += i & (-i);   // update row index to that of parent
37     }
38   }
39 
40   int getSum(int i, int j) {
41     int sum = 0;
42 
43     i++; j++;
44     while (i > 0) {
45       int k = j;
46       while (k > 0) {
47         sum += BITree[i][k]; // accumulate the sum
48         k -= k & (-k); // move column index to parent node
49       }
50       i -= i & (-i);   // move row index to parent node
51     }
52     return sum;
53   }
54 
55   public int sumRegion(int i1, int j1, int i2, int j2) {
56     return getSum(i2, j2) - getSum(i1-1, j2) - getSum(i2, j1-1) + getSum(i1-1, j1-1);
57   }
58 }
59 
60 
61 // Your NumMatrix object will be instantiated and called as such:
62 // NumMatrix numMatrix = new NumMatrix(matrix);
63 // numMatrix.sumRegion(0, 1, 2, 3);
64 // numMatrix.update(1, 1, 10);
65 // numMatrix.sumRegion(1, 2, 3, 4);

 

Leetcode: Range Sum Query 2D - Mutable

原文:http://www.cnblogs.com/EdwardLiu/p/5138208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!