#include
#include
using namespace std;
bool compare(pair<int,int>p1,pair<int,int>p2)
{
return p1.second<p2.second;
}
int main()
{
int t,n,n1,n2;
int mx,count;
cin>>t;
while(t–)
{
mx=0;
cin>>n;
pair<int,int>p[n];
for(int i=0;i<n;i++)
{
cin>>n1>>n2;
p[i]=make_pair(n1,n2);
}
sort(p,p+n,compare);
for(int i=0;i<n;i++){
count=1;
for(int j=i+1;j<n;j++)
{
if(p[i].second<=p[j].first)
{
i=j;
count++;
}
}
if(count>mx)
mx=count;
}
cout<<mx<<endl;
}
return 0;
}
Is my approach fine or there is any better approach
for(int i=0;i<n;i++){
count=1;
for(int j=i+1;j<n;j++)
{
if(p[i].second<=p[j].first)
{
i=j;
count++;
}
You don’t need to do it in a loop, a single traversal is enough,i.e., you need to pick greedily.
Hint you will always pick the first activity in your solution.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.