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:

  1. The matrix is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRegion function is distributed evenly.
  3. You may assume that row 1 ≤ row 2 and col 1 ≤ col 2.

Solution

(1) Java

class NumMatrix {
    private int[][] matrix;
    private int[][] bit;

    public NumMatrix(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return;
        }
        this.matrix = new int[matrix.length][matrix[0].length];
        this.bit = new int[matrix.length+1][matrix[0].length+1];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                update(i, j, matrix[i][j]);
            }
        }
    }

    public void update(int row, int col, int val) {
        int diff = val - matrix[row][col];
        for (int i = row+1; i < bit.length; i += i&(-i)) {
            for(int j = col+1; j < bit[0].length; j += j&(-j)) {
                bit[i][j] += diff;
            }
        }
        matrix[row][col] = val;
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        return getSum(row2, col2) + getSum(row1-1, col1-1) - getSum(row2, col1-1) - getSum(row1-1,col2);
    }

    private int getSum(int row, int col) {
        int sum = 0;
        for (int i = row+1; i > 0; i -= i&(-i)) {
            for(int j = col+1; j > 0; j -= j&(-j)) {
                sum += bit[i][j];
            }
        }
        return sum;
    }
}

/**
 * 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);
 */

(2) Python

class NumMatrix:

    def __init__(self, matrix):
        """
        :type matrix: List[List[int]]
        """
        if not matrix or not matrix[0]:
            return
        self.m = len(matrix)
        self.n = len(matrix[0])
        self.sums = [[0 for _ in range(self.n)] for _ in range(self.m)]
        self.matrix = matrix
        for i in range(self.m):
            for j in range(self.n):
                if j == 0:
                    self.sums[i][j] = matrix[i][j]
                else:
                    self.sums[i][j] = self.sums[i][j-1]+self.matrix[i][j]

    def update(self, row, col, val):
        """
        :type row: int
        :type col: int
        :type val: int
        :rtype: void
        """
        diff = val - self.matrix[row][col]
        self.matrix[row][col] = val
        for i in range(col, self.n):
            self.sums[row][i] += diff

    def sumRegion(self, row1, col1, row2, col2):
        """
        :type row1: int
        :type col1: int
        :type row2: int
        :type col2: int
        :rtype: int
        """
        rst = 0
        for i in range(row1, row2+1):
            if col1 == 0:
                rst += self.sums[i][col2]
            else:
                rst += self.sums[i][col2]-self.sums[i][col1-1]
        return rst


# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# obj.update(row,col,val)
# param_2 = obj.sumRegion(row1,col1,row2,col2)

(3) Scala



results matching ""

    No results matching ""