Search⌘ K
AI Features

DIY: Range Sum Query 2D — Immutable

Explore how to implement the NumMatrix class to efficiently calculate the sum of elements in a submatrix defined by row and column boundaries. Learn to handle multiple queries on a 2D matrix with constraints and return the sum within any rectangular region, a common problem in coding interviews.

Problem statement

Given an m * n matrix, you need to 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)(row1, col1) ...