-
Notifications
You must be signed in to change notification settings - Fork 3
/
1504.cpp
38 lines (37 loc) · 1.03 KB
/
1504.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
31
32
33
34
35
36
37
38
class Solution {
public:
int counting(vector<int>& array) {
int n = array.size();
stack<int> st;
vector<int> sum(n, 0);
for (int i = 0; i < n; ++i) {
while (!st.empty() && array[st.top()] >= array[i]) st.pop();
if (!st.empty()) {
int prevIdx = st.top();
sum[i] = sum[prevIdx];
sum[i] += array[i] * (i - prevIdx);
}
else {
sum[i] = array[i] * (i + 1); // i - (-1)
}
st.push(i);
}
int res = 0;
for (auto& num : sum) res += num;
return res;
}
int numSubmat(vector<vector<int>>& mat) {
int M = mat.size();
int N = mat[0].size();
int res = 0;
vector<int> h(N, 0);
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (mat[i][j] == 0) h[j] = 0;
else h[j]++;
}
res += counting(h);
}
return res;
}
};