-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path221.cpp
41 lines (38 loc) · 1.05 KB
/
221.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
39
40
41
#include<bits/stdc++.h>
using namespace std;
struct bin {
int height, index;
};
int maximalSquare(vector<vector<char>>& matrix) {
int row = matrix.size(), column = matrix[0].size();
vector<int> heights(column, 0);
int area = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;
}
area = max(area, maxArea(heights));
}
return area;
}
int maxArea (vector<int>& height) {
stack<bin> st;
int maxsquare = 0, edge, i;
for (i = 0; i < height.size();) {
if (st.empty() || height[i] >= st.top().height)
st.push(bin{height[i], i++});
else {
int top = st.top().height;
st.pop();
edge = st.empty() ? min(top, i) : min(top, i-st.top().index-1);
maxsquare = max(edge * edge, maxsquare);
}
}
while (!st.empty()) {
int top = st.top().height;
st.pop();
edge = st.empty() ? min(top, i) : min(top, i-st.top().index-1);
maxsquare = max(edge * edge, maxsquare);
}
return maxsquare;
}