-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1504.cpp
More file actions
executable file
·44 lines (37 loc) · 888 Bytes
/
LC1504.cpp
File metadata and controls
executable file
·44 lines (37 loc) · 888 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
35
36
37
38
39
40
41
42
43
44
/*
Problem Statement: https://leetcode.com/problems/count-submatrices-with-all-ones/
Time: O(m • n)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
int numSubmat(vector<vector<int>>& mat) {
int rects, m, n;
rects = 0;
m = mat.size();
n = mat[0].size();
vector<int> h(n);
// histogram model
for (int i = 0; i < m; i++) {
stack<int> st;
vector<int> dp(n);
// update heights of rows
for (int j = 0; j < n; j++)
h[j] = mat[i][j] * (h[j] + 1);
// dynamic programming
for (int j = 0; j < n; j++) {
// monotonic stack with non-decreasing heights
while (!st.empty() && h[st.top()] >= h[j])
st.pop();
if (st.empty())
dp[j] = h[j] * (j + 1);
else
dp[j] = dp[st.top()] + h[j] * (j - st.top());
rects += dp[j];
st.push(j);
}
}
return rects;
}
};