In this Leetcode Range Sum Query 2D - Immutable problem solution You are given a 2D matrix, handle multiple queries of the following type:

Calculate the sum of the elements of the matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Implement the NumMatrix class:

  1. NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
  2. int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Leetcode Range Sum Query 2D - Immutable problem solution


Problem solution in Python.

class NumMatrix:

    def __init__(self, matrix: List[List[int]]):
        self.matrix = matrix
        

    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        matrix_sum = 0 #init matrix sum to 0
        for row in range(row1,row2+1): #for rows specified 
            #add to matrix_sum the sum of the row elements between the two columns
            matrix_sum += sum(self.matrix[row][col1:col2+1]) 
        return matrix_sum



Problem solution in Java.

class NumMatrix {
    int[][] arr;
    void add(int x, int y, int v) {
        arr[x][y] = arr[x-1][y] + arr[x][y-1] + v - arr[x-1][y-1];
    }
    int query(int x1, int y1, int x2, int y2) {
        return arr[x2][y2] - arr[x1-1][y2] - arr[x2][y1-1] + arr[x1-1][y1-1];
    }
    public NumMatrix(int[][] matrix) {
        int n = matrix.length;
        if (n == 0) return;
        int m = matrix[0].length;
        arr = new int[n+1][m+1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                add(i, j, matrix[i-1][j-1]);
            }
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        return query(row1+1, col1+1, row2+1, col2+1);
    }
}


Problem solution in C++.

class NumMatrix {
public:
    NumMatrix(vector<vector<int>> &matrix) {
        _row = matrix.size();
        if (0 == _row) return;
        _col = matrix[0].size();
        tree = vector<vector<int>>(_row + 1, vector<int>(_col + 1, 0));
        for (int i = 0; i < _row; i++) 
         for (int j = 0; j < _col; j++) {
            updateTree(i + 1, j + 1, matrix[i][j]);
        }
    }
    int sumRegion(int row1, int col1, int row2, int col2) {
        return read(row2 + 1, col2 + 1) - read(row1, col2 + 1) - read(row2 + 1, col1) + read(row1, col1);
    }
    
private:
    vector<vector<int>> tree;
    int _row;
    int _col;
    
    void update(int row, int col, int val) {
        updateTree(row, col, val);
    }
    void updateTree(int row, int col, int val) {
        for (int i = row ; i <= _row; i += i & (-i)) 
          for (int j = col; j <= _col; j += j & (-j)) {
                tree[i][j] += val;
        }
    }
    int read(int row, int col) {
        int sum = 0;
        for (int i = row; i > 0; i -= i & (-i))
         for (int j = col; j > 0; j -= j & (-j)) {
                sum += tree[i][j];
        }
        return sum;
    }
};