Skip to content

Commit 810d317

Browse files
committed
Added
1 parent 82331a0 commit 810d317

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
/*Implementation of Maximum size rectangle binary sub-matrix with all 1s GFG
3+
4+
Given a binary matrix, find the maximum size rectangle binary-sub-matrix with all 1’s.
5+
6+
Link of the problem: https://www.geeksforgeeks.org/maximum-size-rectangle-binary-sub-matrix-1s/
7+
8+
*/
9+
10+
#include <iostream>
11+
#include <vector>
12+
#include <stack>
13+
using namespace std;
14+
15+
class MaximalRectangle
16+
{
17+
public:
18+
//In this function, we find next smallest element of each element
19+
vector<int> NSE(vector<int> v)
20+
{
21+
vector<int> nse(v.size(), v.size());
22+
stack<int> stk;
23+
for(int i=0; i<v.size(); i++)
24+
{
25+
while(!stk.empty() && v[stk.top()]>v[i])
26+
{
27+
nse[stk.top()]=i;
28+
stk.pop();
29+
}
30+
stk.push(i);
31+
}
32+
return nse;
33+
}
34+
35+
//In this function, we will find previous smallest element of each element
36+
vector<int> PSE(vector<int> v)
37+
{
38+
vector<int> pse(v.size(), -1);
39+
stack<int> stk;
40+
for(int i=v.size()-1; i>=0; i--)
41+
{
42+
while(!stk.empty() && v[stk.top()]>v[i])
43+
{
44+
pse[stk.top()]=i;
45+
stk.pop();
46+
}
47+
stk.push(i);
48+
}
49+
return pse;
50+
}
51+
52+
int maximalRectangle(vector<vector<char>>& matrix)
53+
{
54+
if(matrix.empty()) return {};
55+
56+
int r= matrix.size();
57+
int c= matrix[0].size();
58+
59+
if(r==0)
60+
return 0;
61+
62+
if(r == 1 && c == 1)
63+
return matrix[0][0] - '0';
64+
65+
vector<vector<int>> v(r, vector<int>(c));
66+
67+
for (int i = 0; i <r; i++)
68+
{
69+
for (int j = 0; j < c; j++)
70+
v[i][j]= matrix[i][j]=='1'?1: 0;
71+
}
72+
73+
for (int i = 1; i < r; i++)
74+
{
75+
76+
for (int j = 0; j < c; j++)
77+
if (v[i][j])
78+
v[i][j] += v[i - 1][j];
79+
}
80+
81+
int area;
82+
for(int i=0; i<r ;i++)
83+
{
84+
vector<int> nse=NSE(v[i]);
85+
vector<int> pse=PSE(v[i]);
86+
87+
for(int j=0;j<c;j++)
88+
area = max(area, v[i][j]*(nse[j]-pse[j]-1));
89+
}
90+
return area;
91+
}
92+
93+
94+
};
95+
96+
int main()
97+
{
98+
MaximalRectangle ob;
99+
100+
int r,c;
101+
char value;
102+
cin>>r;
103+
cin>>c;
104+
vector<vector<char>> v(r);
105+
106+
for(int i=0;i<r;i++)
107+
{
108+
for(int j=0;j<c;j++)
109+
{
110+
cin>>value;
111+
v[i].push_back(value);
112+
}
113+
}
114+
int area = ob.maximalRectangle(v);
115+
cout<<area;
116+
}
117+
118+
/*
119+
120+
Time Complexity: O(R x C)
121+
Only one traversal of the matrix is required, so the time complexity is O(R X C)
122+
123+
Space Complexity: O(C)
124+
Stack is required to store the columns, so so space complexity is O(C)
125+
126+
Example 1:
127+
128+
Input:
129+
0 1 1 0
130+
1 1 1 1
131+
1 1 1 1
132+
1 1 0 0
133+
134+
Output : 8
135+
136+
Explanation :
137+
The largest rectangle with only 1's is from
138+
(1, 0) to (2, 3) which is
139+
1 1 1 1
140+
1 1 1 1
141+
142+
Example 2:
143+
144+
Input:
145+
0 1 1
146+
1 1 1
147+
0 1 1
148+
149+
Output: 6
150+
151+
Explanation :
152+
The largest rectangle with only 1's is from
153+
(0, 1) to (2, 2) which is
154+
1 1
155+
1 1
156+
1 1
157+
158+
*/

0 commit comments

Comments
 (0)