-
Notifications
You must be signed in to change notification settings - Fork 3
/
85.cpp
37 lines (37 loc) · 1.14 KB
/
85.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
class Solution {
public:
int maxArea = 0;
int maximalRectangle(vector<vector<char>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<int> dp(n, 0);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == '1') dp[j] += 1;
else dp[j] = 0;
}
maxHistogram(dp);
}
return maxArea;
}
void maxHistogram(vector<int>& heights) {
stack<int> st;
st.push(-1);
int n = heights.size();
for (int i = 0; i < n; ++i) {
while (st.top() != -1 && heights[st.top()] > heights[i]) {
int currentHeight = heights[st.top()];
st.pop();
int currentWidth = i - 1 - st.top();
maxArea = max(maxArea, currentHeight * currentWidth);
}
st.push(i);
}
while (st.top() != -1) {
int currentHeight = heights[st.top()];
st.pop();
int currentWidth = n - 1 - st.top();
maxArea = max(maxArea, currentHeight * currentWidth);
}
}
};