15% off all plans & roadmaps Use code
See plansProblem editorial
Implement a class named NumMatrix for an integer matrix that remains unchanged after construction. If the matrix has m rows and n columns, rows are numbered from 0 to m - 1 and columns from 0 to n - 1.
Range Sum Query 2D - Immutable is a Array/Matrix Manipulation Patterns problem, in the Prefix Sum - Subarray Sum / Range Query subpattern. The pattern covers in-place operations, hashing, prefix sums, spiral traversal, and cyclic sort
The reference solution runs in O(mn) preprocessing and O(1) per sumRegion query time and O(mn) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Range Sum Query 2D - Immutable is a Array/Matrix Manipulation Patterns problem, in the Prefix Sum - Subarray Sum / Range Query subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.