-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmsakib_01_matrix.cpp
72 lines (63 loc) · 1.39 KB
/
msakib_01_matrix.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
#include<bits/stdc++.h>
using namespace std;
#define R 5
#define C 8
int maxHits(int row[])
{
stack<int>reslt;
int top_val;
int mx_area = 0;
int area = 0;
int i = 0;
while(i<C)
{
if(reslt.empty() || row[reslt.top()] <= row[i])
reslt.push(i++);
else
{
top_val = row[reslt.top()];
reslt.pop();
area = top_val * i;
if(!reslt.empty())
area = top_val * (i - reslt.top() - 1);
mx_area = max(area, mx_area);
}
}
while(!reslt.empty())
{
top_val = row[reslt.top()];
reslt.pop();
area = top_val * i;
if(!reslt.empty())
area = top_val * (i - reslt.top() - 1);
mx_area = max(area, mx_area);
}
return mx_area;
}
int maxRectangle(int A[][C])
{
int reslt = maxHits(A[0]);
for(int i = 1; i < R; i++)
{
for(int j = 0; j < C; j++)
{
if(A[i][j]){
A[i][j] += A[i-1][j];
}
}
reslt = max(reslt, maxHits(A[i]));
}
return reslt;
}
int main()
{
int A[][C] = {
{ 1, 1, 0, 0, 1, 1, 1, 0},
{ 0, 1, 0, 1, 0, 0, 0, 1},
{ 1, 1, 1, 1, 1, 1, 1, 0},
{ 1, 1, 1, 1, 1, 0, 1, 1},
{ 1, 1, 1, 1, 0, 0, 1, 0}
};
cout << "Area of maximum rectangle is "<< maxRectangle(A);
return 0;
}