304. Range Sum Query 2D - Immutable

Practice Range Sum Query 2D - Immutable for free on NudgeCode: Medium, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

The constructor receives a grid matrix that never changes. sumRegion(row1, col1, row2, col2) returns the sum of every cell inside the rectangle whose top-left corner is (row1, col1) and bottom-right corner is (row2, col2), both included. Each sumRegion call should take O(1) time.

Each test is a list of operations and their arguments; the expected output lists what each call returns (null for calls that return nothing).

Example

["NumMatrix","sumRegion","sumRegion","sumRegion"]
[[[[3,0,1],[5,6,3],[1,2,0]]],[0,0,2,2],[1,1,2,2],[0,1,1,2]]
->  [null,21,11,10]

Constraints