Back to photostream

Range Sum Query 2D - Immutable Leetcode Solution

Problem Statement

Range Sum Query 2D - Immutable Leetcode Solution - Given a 2D matrix matrix, handle multiple queries of the following type:

 

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

 

Implement the NumMatrix class:

 

- NumMatrix(int matrix) Initializes the object with the integer matrix matrix.

- 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).

 

Example

 

Input

 

, , , , ]], , , ]

 

Output

 

Explanation

NumMatrix numMatrix = new NumMatrix(, , , , ]);

numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)

numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)

numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)

Constraints

 

- m == matrix.length

- n == matrix.length

- 1

 

www.tutorialcup.com/interview/matrix/range-sum-query-2d-i...

20 views
0 faves
0 comments
Uploaded on May 3, 2022