Description
Given a 2D matrix, handle queries sumRegion(row1, col1, row2, col2) which returns the sum of elements within the rectangle.
Examples
Input:
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)Output:
8Explanation:
Sum of rectangle from (2,1) to (4,3).
Input:
matrix = [[1,2],[3,4]], sumRegion(0, 0, 1, 1)Output:
10Explanation:
Sum of the entire 2x2 matrix. Rectangle from (0,0) to (1,1) includes all elements: 1 + 2 + 3 + 4 = 10.
Input:
matrix = [[7,3,8,1,5],[2,9,4,6,3],[1,5,2,8,7],[4,3,9,2,1]], sumRegion(1, 2, 2, 4)Output:
30Explanation:
Sum of rectangle from (1,2) to (2,4). This includes elements: 4+6+3 (row 1, cols 2-4) + 2+8+7 (row 2, cols 2-4) = 13 + 17 = 30.
Constraints
- •
m == matrix.length - •
n == matrix[i].length - •
1 ≤ m, n ≤ 200