-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
35 lines (32 loc) · 1.15 KB
/
Copy pathsolution.cpp
File metadata and controls
35 lines (32 loc) · 1.15 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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxArea(vector<vector<int>> &mat) {
if (mat.empty() || mat[0].empty()) return 0;
int n = mat.size();
int m = mat[0].size();
vector<int> heights(m, 0);
int maxArea = 0;
// process each row as histogram base
for (int i = 0; i < n; ++i) {
// update heights: consecutive ones up to this row
for (int j = 0; j < m; ++j) {
heights[j] = (mat[i][j] == 1) ? heights[j] + 1 : 0;
}
// compute largest rectangle in histogram heights
stack<int> st;
for (int j = 0; j <= m; ++j) {
int cur = (j == m ? 0 : heights[j]); // sentinel zero at end
while (!st.empty() && heights[st.top()] > cur) {
int h = heights[st.top()];
st.pop();
int width = st.empty() ? j : (j - st.top() - 1);
maxArea = max(maxArea, h * width);
}
st.push(j);
}
}
return maxArea;
}
};