-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_87.cpp
More file actions
37 lines (33 loc) Β· 1.21 KB
/
Copy pathlec10_87.cpp
File metadata and controls
37 lines (33 loc) Β· 1.21 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
class Solution {
public:
int largestRectangleArea(vector<int>& histo) {
stack<int> st;
int maxA = 0;
int n = histo.size();
for(int i = 0; i <= n; i++) {
while (!st.empty() && (i == n || histo[st.top()] >= histo[i])) {
int height = histo[st.top()];
st.pop();
int width = st.empty() ? i : i - st.top() - 1;
maxA = max(maxA, width * height);
}
st.push(i);
}
return maxA;
}
int maximalRectangle(vector<vector<char>>& mat) {
int n = mat.size();
if (n == 0) return 0; // Edge case: empty matrix
int m = mat[0].size();
int maxArea = 0;
vector<int> height(m, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
height[j] = (mat[i][j] == '1') ? height[j] + 1 : 0; // β
Fixed comparison
}
int area = largestRectangleArea(height);
maxArea = max(maxArea, area);
}
return maxArea;
}
};