-
Notifications
You must be signed in to change notification settings - Fork 0
/
#136.cpp
70 lines (57 loc) · 1.46 KB
/
#136.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
70
#include<iostream>
#include<deque>
#include<cstdlib>
#include<vector>
using namespace std;
// Find area of largest consecutive block in histogram array
int largest_area_in_array(vector<int>& arr) {
deque<int> stack;
int max_area = 0;
int i = 0;
while (i < arr.size()) {
if (stack.size() == 0 || arr[i] >= arr[stack.back()]) {
stack.push_back(i++);
} else {
int top_val = arr[stack.back()];
stack.pop_back();
int area = top_val * (stack.size() == 0 ? i : i - stack.back() - 1);
max_area = max(max_area, area);
}
}
while (stack.size() > 0) {
int top_val = arr[stack.back()];
stack.pop_back();
// here, i == arr.size()
int area = top_val * (stack.size() == 0 ? i : i - stack.back() - 1);
max_area = max(max_area, area);
}
return max_area;
}
int area_of_largest_rect(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<int> row(cols, 0);
int max_rect = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (matrix[r][c] == 1)
row[c]++;
else
row[c] = 0;
}
int area = largest_area_in_array(row);
max_rect = max(max_rect, area);
}
return max_rect;
}
int main() {
vector<vector<int>> matrix({
{1,0,0,0},
{1,0,1,1},
{1,0,1,1},
{0,1,0,0}
});
int arr[] = {0,2,3,2,1,1,1,1};
// cout << "Largest rectangle area: " << largest_area_in_array(arr, sizeof(arr)/sizeof(arr[0])) << endl;
cout << "Largest rectangle area: " << area_of_largest_rect(matrix) << endl;
}