-
Notifications
You must be signed in to change notification settings - Fork 3
/
363.cpp
30 lines (28 loc) · 980 Bytes
/
363.cpp
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
class Solution {
public:
int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
int m = matrix.size();
int n = matrix[0].size();
int res = INT_MIN;
for (int i = 0; i < m; ++i) {
vector<int> sum(n, 0); // prefixSum of matrix[j:j+(n), k]
for (int j = i; j < m; ++j) {
for (int t = 0; t < n; ++t) {
sum[t] += matrix[j][t];
}
int currSum = 0;
set<int> st;
st.insert(currSum);
for (int t = 0; t < n; ++t) {
currSum += sum[t];
// [...:k] - [...:i] <= k
// => [...:i] >= [...:k]- k
auto it = st.lower_bound(currSum - k);
if (it != st.end()) res = max(res, currSum - *it);
st.insert(currSum);
}
}
}
return res;
}
};