-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumSubmatrixSumTarget.cpp
More file actions
27 lines (25 loc) · 1.03 KB
/
numSubmatrixSumTarget.cpp
File metadata and controls
27 lines (25 loc) · 1.03 KB
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
class Solution {
public:
int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) {
int m = matrix.size(), n = matrix[0].size(), ans = 0;
vector<vector<int> > prefix(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j)
prefix[i][j] = matrix[i - 1][j - 1] + prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1];
}
for (int i = 1; i <= m; ++i) {
for (int j = i; j <= m; ++j) {
unordered_map<int, int> cnt;
cnt[0] = 1;
for (int k = 1; k <= n; ++k) {
int temp = prefix[j][k] - prefix[i - 1][k];
if (cnt.find(temp - target) != cnt.end())
ans += cnt[temp - target];
++cnt[temp];
}
}
}
return ans;
}
};
// https://leetcode.cn/problems/number-of-submatrices-that-sum-to-target/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-uttw/