Range Query on Two Dimensional List
Implement a data structure with the following methods:
RangeSum(int[][] matrix)
constructs a new instance with the givenmatrix
.total(int row0, int col0, int row1, int col1)
returns the sum of all cellsmatrix[r][c]
whererow0 ≤ r ≤ row1
andcol0 ≤ c ≤ col1
.
Constraints
n, m ≤ 250
wheren
andm
are the number of rows and columns inmatrix
k ≤ 100,000
wherek
is the number of calls tototal
https://binarysearch.com/problems/Range-Query-on-Two-Dimensional-List
Examples
Example 1
Input
- methods =
['constructor', 'total', 'total']
- arguments =
[[[[1, 2, 3], [4, 5, 6]]], [0, 0, 1, 1], [0, 1, 0, 2]]
Output
- answer =
[None, 12, 5]
Explanation
r = RangeSum([[[1,2,3],[4,5,6]])
r.total(0, 0, 1, 1) == 12 # sum([1, 2], [4, 5]])
r.total(0, 1, 0, 2) == 5 # sum([[2, 3]])
Leave a comment