-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0085_Maximal-Rectangle.cpp
More file actions
54 lines (41 loc) · 1.7 KB
/
0085_Maximal-Rectangle.cpp
File metadata and controls
54 lines (41 loc) · 1.7 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
class Solution {
public:
const int MAXN = 1000000;
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.size() == 0) return 0;
vector<vector<int>> dp(matrix.size(), vector<int>(matrix[0].size(), 0));
for (int col = 0; col < matrix[0].size(); ++col) {
for (int row = 0; row < matrix.size(); ++row) {
if (matrix[row][col] == '0') {
dp[row][col] = 0;
} else {
if (row-1 < 0) dp[row][col] = 1;
else dp[row][col] = dp[row-1][col] + 1;
}
}
}
int ans = -MAXN;
for (int row = 0; row < matrix.size(); ++row) {
ans = max(ans, largestRectangleArea(dp[row]));
}
return ans;
}
int largestRectangleArea(vector<int>& heights) {
stack<pair<int, int>> candicate;
heights.push_back(0);
int ans = 0;
candicate.push(make_pair(-1, 0));
for (int i = 0; i < heights.size(); ++i) {
while(!candicate.empty() && candicate.top().second > heights[i]) {
auto top = candicate.top();
candicate.pop();
//cout << top.first << " " << top.second << endl;
ans = max(ans, (i - candicate.top().first - 1) * top.second);
//cout << i-1 << " back to " << candicate.top().first << " and the value is : " << (i - candicate.top().first - 1) * top.second << endl;
}
candicate.push(make_pair(i, heights[i]));
}
heights.pop_back();
return ans;
}
};