-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlargest-rectangle.cpp
125 lines (97 loc) · 2.9 KB
/
largest-rectangle.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <bits/stdc++.h>
using namespace std;
#define log(x) cout << #x << " = " << x << endl;
void execute() {
int col, row;
cin>>col>>row;
vector<vector<int>> matrix(col, vector<int>(row, 0));
vector<vector<int>> convertedMatrix(col, vector<int>(row, 0));
// input
// 7
// 1 0 1 0 0 0 0
// 0 0 0 0 0 0 1
// 1 0 0 0 0 0 0
// 0 0 0 0 1 0 1
for(int i = 0; i < col; i++ ) {
for(int j = 0; j < row; j++ ) {
cin>>matrix[i][j];
}
}
auto process = [&](int i, int j) -> int {
if ( matrix[i][j] ) { return 0; }
if ( i - 1 < 0 ) { return 1; }
else { return convertedMatrix[i-1][j] + 1; }
};
// intermediate conversions
// 0 1 0 1 1 1 1
// 1 2 1 2 2 2 0
// 0 3 2 3 3 3 1
// 1 4 3 4 0 4 0
for(int i = 0; i < col; i++ )
for(int j = 0; j < row; j++ )
convertedMatrix[i][j] = process(i, j);
vector<vector<int>> left(col, vector<int>(row, 0));
vector<vector<int>> right(col, vector<int>(row, 0));
struct ValIndex {
int index;
int value;
ValIndex(i: int, v: int) {
index = i;
value = v;
}
}
stack<ValIndex> maintainIncreasing;
auto processIncreasingLeft = [&](int i, int j) {
if ( j - 1 ) { return 1; }
while ( maintainIncreasing.top().value < left[i][j] ) {
maintainIncreasing.pop();
}
int distance = 1;
if ( maintainIncreasing.empty() == false ) {
distance = j - maintainIncreasing.top().value;
}
auto toBeInserted = ValIndex(.index: j, .value: convertedMatrix[i][j]);
maintainIncreasing.push(toBeInserted);
return distance;
};
for(int i = 0; i < col; i++ ) {
for(int j = 0; j < row; j++ ) {
left[i][j] = processIncreasingLeft(i, j);
}
}
auto processIncreasingRight = [&](int i, int j) {
if ( j - 1 ) { return 1; }
while ( maintainIncreasing.top().value < left[i][j] ) {
maintainIncreasing.pop();
}
int distance = 1;
if ( maintainIncreasing.empty() == false ) {
distance = j - maintainIncreasing.top().value;
}
auto toBeInserted = ValIndex(.index: j, .value: convertedMatrix[i][j]);
maintainIncreasing.push(toBeInserted);
return distance;
};
for(int i = 0; i < col; i++ ) {
for(int j = 0; j < row; j++ ) {
left[i][j] = processIncreasingRight(i, j);
}
}
for(int i = 0; i < col; i++ ) {
for(int j = 0; j < row; j++ ) {
cout<< left[i][j]<<" ";
}
cout<<endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
freopen("input.txt", "r", stdin);
int no_of_test_cases = 1;
// cin >> no_of_test_cases;
while (no_of_test_cases--) {
execute();
}
return 0;
}