-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY7
More file actions
37 lines (27 loc) · 903 Bytes
/
DAY7
File metadata and controls
37 lines (27 loc) · 903 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
//number-of-submatrices-that-sum-to-target
class Solution {
public:
int numSubmatrixSumTarget(std::vector<std::vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
for (int row = 0; row < m; row++) {
for (int col = 1; col < n; col++) {
matrix[row][col] += matrix[row][col - 1];
}
}
int count = 0;
for (int c1 = 0; c1 < n; c1++) {
for (int c2 = c1; c2 < n; c2++) {
std::unordered_map<int, int> map;
map[0] = 1;
int sum = 0;
for (int row = 0; row < m; row++) {
sum += matrix[row][c2] - (c1 > 0 ? matrix[row][c1 - 1] : 0);
count += map[sum - target];
map[sum]++;
}
}
}
return count;
}
};