Problem Statement:

Given a 2D matrix matrix, handle multiple queries of the following type:

Implement the NumMatrix class:

You must design an algorithm where sumRegion works on O(1) time complexity.

Example:

Input:
["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
[[[[3, 0, 1, 4, 2], 
   [5, 6, 3, 2, 1], 
   [1, 2, 0, 1, 5], 
   [4, 1, 0, 1, 7], 
   [1, 0, 3, 0, 5]]], 
[2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]]

Output:
[null, 8, 11, 12]

Intuition:


Code:

My Solution