-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay59.cpp
More file actions
34 lines (34 loc) · 938 Bytes
/
Day59.cpp
File metadata and controls
34 lines (34 loc) · 938 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
class Solution {
public:
int solve(vector<int> &temp, int target){
int cnt = 0;
int sum = 0;
unordered_map<int, int> mp;
for(int i=0;i<temp.size();i++){
sum += temp[i];
if(sum == target){
cnt++;
}
if(mp.find(sum- target) != mp.end()){
cnt += mp[sum - target];
}
mp[sum]++;
}
return cnt;
}
int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int ans = 0;
for(int i=0;i<m;i++){
vector<int> temp(n, 0);
for(int j=i;j<m;j++){
for(int k=0;k<n;k++){
temp[k] += matrix[j][k];
}
ans += solve(temp, target);
}
}
return ans;
}
};