-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximal-Rectangle.cpp
More file actions
68 lines (56 loc) · 1.8 KB
/
Copy pathMaximal-Rectangle.cpp
File metadata and controls
68 lines (56 loc) · 1.8 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public:
vector<int> getNSL(vector<int>& heights) {
int n = heights.size();
stack<int> st;
vector<int> left(n);
for (int i = 0; i < n; i++) {
while (!st.empty() && heights[st.top()] >= heights[i]) st.pop();
if (st.empty()) left[i] = -1;
else left[i] = st.top();
st.push(i);
}
return left;
}
vector<int> getNSR(vector<int>& heights) {
int n = heights.size();
stack<int> st;
vector<int> right(n);
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && heights[st.top()] >= heights[i]) st.pop();
if (st.empty()) right[i] = n;
else right[i] = st.top();
st.push(i);
}
return right;
}
int findMaxArea(vector<int>& heights) {
vector<int> NSR = getNSR(heights);
vector<int> NSL = getNSL(heights);
int area = 0;
for (int i = 0; i < heights.size(); i++) {
area = max(area, (heights[i] * (NSR[i] - NSL[i] - 1)));
}
return area;
}
int maximalRectangle(vector<vector<char>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
vector<int> height(m);
for (int i = 0; i < m; i++) {
height[i] = (matrix[0][i] == '1') ? 1 : 0;
}
int maxArea = findMaxArea(height);
for (int row = 1; row < n; row++) {
for (int col = 0; col < m; col++) {
if (matrix[row][col] == '0') {
height[col] = 0;
} else {
height[col] += 1;
}
}
maxArea = max(maxArea, findMaxArea(height));
}
return maxArea;
}
};