Advertisement
exmkg

Untitled

Aug 5th, 2024
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. class NumMatrix {
  2.     int[][] sum;
  3.  
  4.     public NumMatrix(int[][] matrix) {
  5.         int m = matrix.length;
  6.         int n = matrix[0].length;
  7.  
  8.         // sum[i][j] is the sum of all elements
  9.         // inside the prefix-submatrix matrix[1 .. i][1 .. j].
  10.         //
  11.         // `sum` is a 1-indexed matrix whereas `matrix` is 0-indexed.
  12.         // This is intentional to simplify computations.
  13.         // If you make `sum` matrix 0-indexed, you will have to
  14.         // write many `if` statements to check that you didn't go out-of-bounds.
  15.         //
  16.         // Assume its 0-th row (sum[0][...]) and 0-th column (sum[...][0]) are filled with 0s.
  17.         // That indeed greatly simplifies computations.
  18.         sum = new int[m + 1][n + 1];
  19.  
  20.         for (int i = 1; i <= m; i++) {
  21.             for (int j = 1; j <= n; j++) {
  22.                 // sum of all elems from (1, 1) till (i, j) =
  23.                 sum[i][j] = (sum[i - 1][j] +        // + sum(matrix[1 .. i - 1][1 .. j    ])  ~> add sum of all elems from (1, 1) till (i - 1, j)     (+)
  24.                              sum[i][j - 1] -        // + sum(matrix[1 .. i    ][1 .. j - 1])  ~> add sum of all elems from (1, 1) till (i, j - 1)     (+)
  25.                              sum[i - 1][j - 1]) +   // - sum(matrix[1 .. i - 1][1 .. j - 1])  ~> sub sum of all elems from (1, 1) till (i - 1, j - 1) because they were added twice
  26.                              matrix[i - 1][j - 1];  // + matrix[i][j]                         ~> add the value in cell (i, j)
  27.                 // matrix[i - 1][j - 1] because `matrix` is actually 0-indexed.
  28.                 // Please draw the `sum` matrix on a piece of paper and compute it by hand step by step to understand.
  29.             }
  30.         }
  31.     }
  32.  
  33.     public int sumRegion(int r1, int c1, int r2, int c2) {
  34.         // Since our `sum` matrix is 1-indexed and given r1, c1, r2, c2 are 0-indexed, we increase r1, c1, r2, c2 by 1.
  35.         r1++; c1++; r2++; c2++;
  36.         return (sum[r2][c2] -          // + sum(matrix[1 .. r2][1 .. c2])          ~> add sum of all elems from (1, 1) till (r2, c2)         (+)
  37.                 sum[r2][c1 - 1] -      // - sum(matrix[1 .. r2][1 .. c1 - 1])      ~> sub sum of all elems from (1, 1) till (r2, c1 - 1)     (-)
  38.                 sum[r1 - 1][c2] +      // - sum(matrix[1 .. r1 - 1][1 .. c2])      ~> sub sum of all elems from (1, 1) till (r1 - 1, c2)     (-)
  39.                 sum[r1 - 1][c1 - 1]);  // + sum(matrix[1 .. r1 - 1][1 .. c1 - 1])  ~> add sum of all elems from (1, 1) till (r1 - 1, c1 - 1) because they were added once and twice subbed
  40.     }
  41. }
  42.  
  43. /**
  44.  * Your NumMatrix object will be instantiated and called as such:
  45.  * NumMatrix obj = new NumMatrix(matrix);
  46.  * int param_1 = obj.sumRegion(row1,col1,row2,col2);
  47.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement