-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathMaximalRectangle.cpp
43 lines (37 loc) · 1.02 KB
/
MaximalRectangle.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
42
43
// 85. Leetcode {HARD} [DP , Arrays , Stack]
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int maximalRectangle(vector<vector<char>> &matrix)
{
if (matrix.size() == 0)
return 0;
int ans = 0;
int m = matrix.size(); // rows
int n = matrix[0].size(); // columns
vector<int> height(n, 0); // height
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (matrix[i][j] == '0')
{
height[j] = 0;
continue;
}
height[j]++;
for (int cur = j - 1, pre = height[j]; cur >= 0; cur--)
{
if (height[cur] == 0)
break;
pre = min(pre, height[cur]);
ans = max(ans, (j - cur + 1) * pre);
}
ans = max(ans, height[j]);
}
}
return ans;
}
};