-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumMatrix.py
More file actions
21 lines (18 loc) · 720 Bytes
/
Copy pathNumMatrix.py
File metadata and controls
21 lines (18 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class NumMatrix:
def __init__(self, matrix: List[List[int]]):
self.matrix = matrix
self.prefix = []
sum_ = 0
for i in range(len(matrix)):
self.prefix.append([])
for j in range(len(matrix[i])):
sum_ += matrix[i][j]
self.prefix[i].append(sum_)
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
sum_ = 0
for i in range(row1,row2+1):
sum_ += self.prefix[i][col2] - self.prefix[i][col1] + self.matrix[i][col1]
return sum_
# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)