class NumMatrix:
'''
DP
'''
def __init__(self, matrix: List[List[int]]):
M, N = len(matrix), len(matrix) and len(matrix[0])
self.AGG = [[0] * (N+1) for _ in range(M+1)]
for i in range(M):
for j in range(N):
self.AGG[i+1][j+1] = matrix[i][j] + self.AGG[i][j+1] + self.AGG[i+1][j] - self.AGG[i][j]
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
return self.AGG[row2+1][col2+1] + self.AGG[row1][col1] - self.AGG[row1][col2+1] - self.AGG[row2+1][col1]
# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)