-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangeSumQuery2D-Immutable.py
More file actions
27 lines (21 loc) · 951 Bytes
/
rangeSumQuery2D-Immutable.py
File metadata and controls
27 lines (21 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/range-sum-query-2d-immutable/
# Author: Miao Zhang
# Date: 2021-02-01
class NumMatrix:
def __init__(self, matrix: List[List[int]]):
if not matrix:
self.dp = [[0]]
return
m = len(matrix)
n = len(matrix[0])
self.dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
self.dp[i][j] = self.dp[i - 1][j] + self.dp[i][j - 1] - self.dp[i - 1][j - 1] + matrix[i - 1][j - 1]
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
return self.dp[row2 + 1][col2 + 1] - self.dp[row2 + 1][col1] - self.dp[row1][col2 + 1] + self.dp[row1][col1]
# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)