Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5c68168

Browse files
Create 1074.py
1 parent 32602e4 commit 5c68168

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

1001-1500/1074.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def numSubmatrixSumTarget(self, matrix: List[List[int]], target: int) -> int:
3+
m, n = len(matrix), len(matrix[0])
4+
for row in range(m):
5+
for col in range(1, n):
6+
matrix[row][col] += matrix[row][col - 1]
7+
8+
count = 0
9+
for c1 in range(n):
10+
for c2 in range(c1, n):
11+
prefix_sum_map = {0: 1}
12+
sum_val = 0
13+
14+
for row in range(m):
15+
sum_val += matrix[row][c2] - (matrix[row][c1 - 1] if c1 > 0 else 0)
16+
count += prefix_sum_map.get(sum_val - target, 0)
17+
prefix_sum_map[sum_val] = prefix_sum_map.get(sum_val, 0) + 1
18+
19+
return count

0 commit comments

Comments
 (0)