-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13apr.txt
61 lines (58 loc) · 1.88 KB
/
13apr.txt
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
class Solution {
public:
int findLargestAreaInHistogram(int n, vector<int>& rowHistogram) {
int area = 0;
for (int i = 0; i < n; i++) {
int breadth = 1;
// first find only this area
area = max(area, rowHistogram[i] * breadth);
// now start checking for other lengths as well
// as we might need other heights for more area
// first check for heights behind it
for (int j = i - 1; j >= 0; j--) {
if (rowHistogram[j] >= rowHistogram[i]) {
breadth++;
} else {
break;
}
}
// now check for heights after it
for (int j = i + 1; j < n; j++) {
if (rowHistogram[j] >= rowHistogram[i]) {
breadth++;
} else {
break;
}
}
// now compute the area
area = max(area, rowHistogram[i] * breadth);
}
return area;
}
int maximalRectangle(vector<vector<char>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
int area = 0;
// first make the histogram row wise
vector<int> rowHistogram(m, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; ++j) {
if (matrix[i][j] == '1') {
rowHistogram[j]++;
} else {
rowHistogram[j] = 0;
}
}
// now I have created the histogram for the particular level
// now find the largest area in that level
area = max(area, findLargestAreaInHistogram(m, rowHistogram));
}
return area;
}
};
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();