Search⌘ K
AI Features

DIY: Range Sum Query 2D — Immutable

Explore how to implement a num_matrix class in Ruby that supports fast sum queries on subregions of a 2D matrix. Understand how to initialize the matrix and retrieve sums for various rectangular regions, preparing you to handle similar algorithm problems 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) ...