Skip to content

Commit 1fdf93c

Browse files
committed
Sync LeetCode submission Runtime - 117 ms (57.65%), Memory - 31.1 MB (8.46%)
1 parent 920f1f1 commit 1fdf93c

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>Given a 2D matrix <code>matrix</code>, handle multiple queries of the following type:</p>
2+
3+
<ul>
4+
<li>Calculate the <strong>sum</strong> of the elements of <code>matrix</code> inside the rectangle defined by its <strong>upper left corner</strong> <code>(row1, col1)</code> and <strong>lower right corner</strong> <code>(row2, col2)</code>.</li>
5+
</ul>
6+
7+
<p>Implement the <code>NumMatrix</code> class:</p>
8+
9+
<ul>
10+
<li><code>NumMatrix(int[][] matrix)</code> Initializes the object with the integer matrix <code>matrix</code>.</li>
11+
<li><code>int sumRegion(int row1, int col1, int row2, int col2)</code> Returns the <strong>sum</strong> of the elements of <code>matrix</code> inside the rectangle defined by its <strong>upper left corner</strong> <code>(row1, col1)</code> and <strong>lower right corner</strong> <code>(row2, col2)</code>.</li>
12+
</ul>
13+
14+
<p>You must design an algorithm where <code>sumRegion</code> works on <code>O(1)</code> time complexity.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/sum-grid.jpg" style="width: 415px; height: 415px;" />
19+
<pre>
20+
<strong>Input</strong>
21+
[&quot;NumMatrix&quot;, &quot;sumRegion&quot;, &quot;sumRegion&quot;, &quot;sumRegion&quot;]
22+
[[[[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]]
23+
<strong>Output</strong>
24+
[null, 8, 11, 12]
25+
26+
<strong>Explanation</strong>
27+
NumMatrix numMatrix = new NumMatrix([[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]]);
28+
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
29+
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
30+
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>m == matrix.length</code></li>
38+
<li><code>n == matrix[i].length</code></li>
39+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
40+
<li><code>-10<sup>4</sup> &lt;= matrix[i][j] &lt;= 10<sup>4</sup></code></li>
41+
<li><code>0 &lt;= row1 &lt;= row2 &lt; m</code></li>
42+
<li><code>0 &lt;= col1 &lt;= col2 &lt; n</code></li>
43+
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>sumRegion</code>.</li>
44+
</ul>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Approach 4: Caching Smarter
2+
3+
# m = no. of rows, n = no. of cols
4+
# Time: O(1) per query, O(mn) for pre-computation
5+
# Space: O(mn)
6+
7+
class NumMatrix:
8+
9+
def __init__(self, matrix: List[List[int]]):
10+
if not matrix or not matrix[0]:
11+
return
12+
13+
rows, cols = len(matrix), len(matrix[0])
14+
self.dp = [[0] * (cols + 1) for _ in range(rows + 1)]
15+
16+
for r in range(rows):
17+
for c in range(cols):
18+
self.dp[r + 1][c + 1] = self.dp[r + 1][c] \
19+
+ self.dp[r][c + 1] \
20+
- self.dp[r][c] \
21+
+ matrix[r][c]
22+
23+
24+
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
25+
return self.dp[row2 + 1][col2 + 1] \
26+
- self.dp[row1][col2 + 1] \
27+
- self.dp[row2 + 1][col1] \
28+
+ self.dp[row1][col1]
29+
30+
31+
32+
# Your NumMatrix object will be instantiated and called as such:
33+
# obj = NumMatrix(matrix)
34+
# param_1 = obj.sumRegion(row1,col1,row2,col2)

0 commit comments

Comments
 (0)