Skip to content

Commit cf629d1

Browse files
authoredOct 16, 2022
matrix question(c++)
1 parent d01199f commit cf629d1

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
 

‎cpp_matrix/Max_rectangle.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*Given a binary matrix M of size n X m. Find the
2+
maximum area of a rectangle formed only of 1s in
3+
the given matrix.*/
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
#define MAX 1000
7+
8+
9+
10+
class Solution{
11+
public:
12+
int minareahistogram(int a[],int m)
13+
{
14+
int pres[m],nexts[m];
15+
stack<pair<int,int>> s,q;
16+
17+
for(int i=0;i<m;i++)
18+
{
19+
while(s.size()!=0 && s.top().first>=a[i])
20+
{
21+
s.pop();
22+
}
23+
if(s.size()==0)
24+
pres[i]=0;
25+
else
26+
pres[i]=s.top().second;
27+
s.push({a[i],i+1});
28+
}
29+
for(int i=m-1;i>=0;i--)
30+
{
31+
while(!q.empty() && q.top().first>=a[i])
32+
{
33+
q.pop();
34+
}
35+
if(q.empty())
36+
nexts[i]=m+1;
37+
else
38+
nexts[i]=q.top().second;
39+
q.push({a[i],i+1});
40+
}
41+
int ma=0;
42+
for(int i=0;i<m;i++)
43+
{ int p=nexts[i]-pres[i]-1;
44+
ma=max(ma,a[i]*p);
45+
}
46+
return ma;
47+
}
48+
int maxArea(int m[MAX][MAX], int n, int m1) {
49+
int a[m1]={0},ma=0;
50+
for(int i=0;i<n;i++)
51+
{
52+
for(int j=0;j<m1;j++)
53+
{
54+
if(m[i][j]==0)
55+
a[j]=m[i][j];
56+
else
57+
a[j]+=m[i][j];
58+
}
59+
ma=max(ma,minareahistogram(a,m1));
60+
}
61+
return ma;
62+
}
63+
64+
};
65+
66+
67+
int main() {
68+
int T;
69+
cin >> T;
70+
71+
int M[MAX][MAX];
72+
73+
while (T--) {
74+
int n, m;
75+
cin >> n >> m;
76+
77+
for (int i = 0; i < n; i++) {
78+
for (int j = 0; j < m; j++) {
79+
cin >> M[i][j];
80+
}
81+
}
82+
Solution obj;
83+
cout << obj.maxArea(M, n, m) << endl;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//Median in a row-wise sorted Matrix
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
6+
class Solution{
7+
public:
8+
int check(vector<vector<int>> &matrix,int m)
9+
{ int c=0;
10+
for(int i=0;i<matrix.size();i++)
11+
{
12+
int p=upper_bound(matrix[i].begin(),matrix[i].end(),m)-matrix[i].begin();
13+
c+=p;
14+
}
15+
return c;
16+
}
17+
int median(vector<vector<int>> &matrix, int r, int c){
18+
int l=1,r1=2000;
19+
while(l<=r1)
20+
{
21+
int mid=(r1+l)/2;
22+
int p=check(matrix,mid);
23+
if(p<=(r*c)/2)
24+
l=mid+1;
25+
else
26+
r1=mid-1;
27+
}
28+
return l;
29+
}
30+
};
31+
32+
33+
int main()
34+
{
35+
int t;
36+
cin>>t;
37+
while(t--)
38+
{
39+
int r, c;
40+
cin>>r>>c;
41+
vector<vector<int>> matrix(r, vector<int>(c));
42+
for(int i = 0; i < r; ++i)
43+
for(int j = 0;j < c; ++j)
44+
cin>>matrix[i][j];
45+
Solution obj;
46+
cout<<obj.median(matrix, r, c)<<endl;
47+
}
48+
return 0;
49+
}

‎cpp_matrix/Row_with_max_1s.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//Row with max 1s
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
6+
class Solution{
7+
public:
8+
int rowWithMax1s(vector<vector<int> > arr, int n, int m) {
9+
int i=0,j=m-1,r=-1;
10+
11+
while(i<n && j>=0)
12+
{while(arr[i][j]==1)
13+
{
14+
j--;
15+
r=i;
16+
}
17+
i++;
18+
}
19+
return r;
20+
}
21+
22+
};
23+
24+
int main() {
25+
int t;
26+
cin >> t;
27+
while (t--) {
28+
int n, m;
29+
cin >> n >> m;
30+
vector< vector<int> > arr(n,vector<int>(m));
31+
for (int i = 0; i < n; i++) {
32+
for (int j = 0; j < m; j++) {
33+
cin>>arr[i][j];
34+
}
35+
}
36+
Solution ob;
37+
auto ans = ob.rowWithMax1s(arr, n, m);
38+
cout << ans << "\n";
39+
}
40+
return 0;
41+
}

0 commit comments

Comments
 (0)
Please sign in to comment.