Search⌘ K
AI Features

DIY: Range Sum Query 2D — Immutable

Understand how to implement the NumMatrix class that computes the sum of elements within a sub-rectangle of a 2D matrix. Learn methods to initialize the matrix and quickly return sums for given regions, preparing you for coding interview questions involving range queries.

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