forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1074.cpp
More file actions
38 lines (29 loc) · 1023 Bytes
/
1074.cpp
File metadata and controls
38 lines (29 loc) · 1023 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
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) {
int r = matrix.size();
int c = matrix[0].size();
// convert each row into prefix sum
for(int i = 0; i < r; i++)
for(int j = 1; j < c; j++)
matrix[i][j] += matrix[i][j - 1];
int ans = 0;
unordered_map<int, int> m1;
for(int i = 0; i < c; i++) {
for(int j = i; j < c; j++) {
m1.clear();
m1[0]++;
int sum = 0;
for(int k = 0; k < r; k++) {
int temp = matrix[k][j];
if(i > 0)
temp -= matrix[k][i - 1];
sum += temp;
ans += m1[sum - target];
m1[sum]++;
}
}
}
return ans;
}
};