Best ans Most Clean Solution using set<int> and map<int,pait<int,int>>


#1

int Solution::solve(vector &A) {
map<int,pair<int,int>> r,g,b;
set r1,b1,g1;
int m=A.size(),n=A[0].size();
for(int j=0;j<n;j++){
int mr=INT_MAX,mar=INT_MIN,mg=INT_MAX,mag=INT_MIN,mb=INT_MAX,mab=INT_MIN;
for(int i=0;i<m;i++){
if(A[i][j]==‘r’){
mr = min(mr,i);
mar = max(mar,i);
}
if(A[i][j]==‘g’){
mg = min(mg,i);
mag = max(mag,i);
}
if(A[i][j]==‘b’){
mb = min(mb,i);
mab = max(mab,i);
}
}
if(mr!=INT_MAX){
r[j]={mr,mar};
//cout<<r[j].first<<" “;
}
if(mg!=INT_MAX){
g[j]={mg,mag};
//cout<<g[j].first<<” “;
}
if(mb!=INT_MAX){
b[j]={mb,mab};
//cout<<b[j].first<<” “;
}
//cout<<”\n";
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(A[i][j]==‘r’){
r1.insert(j);
}
if(A[i][j]==‘g’){
g1.insert(j);
}
if(A[i][j]==‘b’){
b1.insert(j);
}
}
}
double ans=0;
for(int j=0;j<n;j++){
if(r.find(j)!=r.end() && g.find(j)!=g.end()){
int height = max(abs(j-((–b1.end())))+1,abs(j-((b1.begin())))+1);
int base = max(abs(r[j].first-g[j].second)+1,abs(r[j].second-g[j].first)+1);
ans = max(ans, ceil(0.5baseheight));
}
if(g.find(j)!=g.end() && b.find(j)!=b.end()){
int height = max(abs(j-((–r1.end())))+1,abs(j-((r1.begin())))+1);
int base = max(abs(g[j].first-b[j].second)+1,abs(g[j].second-b[j].first)+1);
ans = max(ans, ceil(0.5baseheight));
}
if(b.find(j)!=b.end() && r.find(j)!=r.end()){
int height = max(abs(j-((–g1.end())))+1,abs(j-((g1.begin())))+1);
int base = max(abs(b[j].first-r[j].second)+1,abs(b[j].second-r[j].first)+1);
ans = max(ans, ceil(0.5baseheight));
}
}
return ans;
}