-
Notifications
You must be signed in to change notification settings - Fork 0
/
00085-maximal-rectangle.cpp
46 lines (42 loc) · 1.58 KB
/
00085-maximal-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
/* hard :: stack */
/*
This function calculates the area of the largest rectangle containing only 1's in
a binary matrix. It uses a histogram-based approach with a monotonic stack to compute
the maximal rectangle row by row.
- - - -
Time :: O(R * C) :: 0ms
Space :: O(C) :: 17.02MB
*/
class Solution {
public:
// Solution entrypoint :: - - - -
int maximalRectangle( vector<vector<char>>& matrix )
{
const uint16_t row = matrix.size();
const uint16_t col = matrix[0].size();
if ( row==1 && col==1 ) {
return matrix[0][0] == '1';
}
vector<uint16_t> h( col + 1 );
int32_t ret = 0;
for ( int32_t i = 0; i < row; i++ )
{
vector<int32_t> mst = { -1 };
for ( int32_t j = 0; j <= col; j++ )
{
// Count the successive '1's & store in h[j] :
h[j] = ( j == col || matrix[i][j] == '0' ) ? 0 : h[j] + 1;
// Monotonic stack has at least element -1 :
while( mst.size() > 1 && ( j == col || h[j] < h[mst.back()] ) )
{
const int32_t m = mst.back();
mst.pop_back();
ret = max( ret, h[m] * ( j - mst.back() - 1 ) );
}
mst.push_back( j );
}
}
return ret;
}
};
// End. :: - - - -