Getting TLE with the same code as in the solution!


#1

Comment body goebool check(vector a,vector b){

return a[1]<b[1];

}

int Solution::solve(vector<vector > &a) {

if(a.size()==0) return 0;

sort(a.begin(),a.end(),check);

int l=a[0][1];
int ans=1;

//latest=a[0];

for(int i=1;i<a.size();i++){
    
    if(a[i][0]<=l) continue;
    else{
        l=a[i][1];
        ans+=1;
    }
    
}
return ans;

}
s here.


#2

Try to use check(vector &a,vector &b )return a[1]<b[1]; instead off check(vector a,vector b)return a[1]<b[1];
because first is pass by reference and second in a pass by value, pass by value takes more time as compare to pass by reference because in a pass by value function create a copy of pass element in our case which is two vector.

I hope this might help you


#3

If you are getting TLE, use the following custom comparator function for sorting the array
bool cmp(vector < int > a,vector < int > b)
{
if(a[1]!=b[1]) return a[1]<b[1];
return a[0]<=b[0];
}