-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1-9.求最大子矩阵的大小.cpp
59 lines (45 loc) · 1.11 KB
/
1-9.求最大子矩阵的大小.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
#include <iostream>
#include <stack>
#include <string>
#define MAX 100
using namespace std;
int maxRecFromBottom( int height[], int n ) {
int maxArea = 0;
stack<int> s;
for( int i = 0; i < n; i++ ) {
while( !s.empty() && height[i] <= height[s.top()] ) {
int j = s.top();
s.pop();
int k = s.empty() ? -1 : s.top();
int curArea = ( i - k - 1 ) * height[j];
maxArea = max( maxArea, curArea );
}
s.push( i );
}
while( !s.empty() ) {
int j = s.top();
s.pop();
int k = s.empty() ? -1 : s.top();
int curArea = ( n - k - 1 ) * height[j];
maxArea = max( maxArea, curArea );
}
return maxArea;
}
int maxRecSize( int map[MAX][MAX], int n, int m ) {
int height[MAX];
int maxArea = 0;
fill( height, height + m, 0 );
for( int i = 0; i < n; i++ ) {
for( int j = 0; j < m; j++ ) {
height[j] = map[i][j] == 0 ? 0 : height[j] + 1;
}
maxArea = max( maxRecFromBottom( height, m ), maxArea );
}
return maxArea;
}
int main() {
int map[MAX][MAX] = { { 1, 0, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 0 } };
int maxArea = maxRecSize( map, 3, 4 );
printf( "%d\n", maxArea);
return 0;
}