1082B. Vova and Trophies - Codeforces Solution C++

  Problem Link : 1082B. Vova and Trophies 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
	int n;
	cin>>n;

	map<int,int> mp;
	int cnt=0;
	char arr[n+1];
	for(int i=1;i<=n;i++)
	{
		cin>>arr[i];
		if(arr[i]=='G')
			cnt++;
	}

	int i=1,j;
	while(i<=n)
	{
		if(arr[i]=='S')
		{
			i++;
			continue;
		}
		j=i;
		while(i<=n && arr[i]=='G')
				i++;

		mp[j]=i;	
	}
	int ans=0;
	auto it2=mp.begin();
	for(auto it=mp.begin();it!=mp.end();it++)
	{
		if(mp.size()>1)
			ans=max(ans,1+(*it).second-(*it).first);
		else
			ans=max(ans,(*it).second-(*it).first);
		it2=it;
		it2++;
		if(it2==mp.end())
			break;
		if((*it2).first==(*it).second+1)
		{
			int len1=(*it).second-(*it).first;
			int len2=(*it2).second-(*it2).first;
			if(len1+len2<cnt)
				ans=max(ans,len1+len2+1);
			else
				ans=max(ans,len1+len2);
		}
	}

	cout<<ans;

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!