-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.cpp
70 lines (64 loc) · 1.92 KB
/
Solution.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
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
69
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <cmath>
using namespace std;
class Solution {
public:
// dp orz
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.size() == 0 or matrix[0].size() == 0) return 0;
vector<int> height (matrix[0].size(), 0);
vector<int> left (matrix[0].size(), 0);
vector<int> right (matrix[0].size(), matrix[0].size()-1);
int max_area = 0;
for (int i=0; i<matrix.size(); i++) {
// update height
for (int j=0; j<matrix[0].size(); j++) {
if (matrix[i][j] == '1') {
height[j] ++ ;
} else {
height[j] = 0;
}
}
// update left
int lb = 0;
for (int j=0; j<matrix[0].size(); j++) {
if (matrix[i][j] == '1') {
left[j] = max(lb, left[j]);
} else {
left[j] = 0;
lb = j + 1;
}
}
// update right
int rb = matrix[0].size() - 1;
for (int j=rb; j>=0; j--) {
if (matrix[i][j] == '1') {
right[j] = min(rb, right[j]);
} else {
right[j] = matrix[0].size() - 1;
rb = j - 1;
}
}
// update max_area
for (int j=0; j<matrix[0].size(); j++) {
if (matrix[i][j] == '1') {
max_area = max(max_area, height[j]*(right[j] - left[j] + 1));
}
}
// for (auto i:left ) cout << i << ' ';
// cout << endl;
}
return max_area;
}
};
int main() {
Solution s;
return 0;
}