Search⌘ K

DIY: Range Sum Query 2D — Immutable

Explore how to implement the NumMatrix class to efficiently compute sum queries on subregions of a 2D matrix. This lesson helps you understand handling multiple range sum queries for immutable matrices, preparing you for similar coding interview problems.

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