Suppose, we have a 2D vector<vector<int>> vec;
We need to sort this 2D vector. I tried with two methods below:
Method 1: Using a comparator function
static bool cmp(vector<int> a, vector<int> b) {
return a[1] < b[1];
}
...
sort(vec.begin(),vec.end(),cmp);
Method 2: Using a lambda
sort(vec.begin(), vec.end(), [](const vector<int>& a, vector<int>& b) {
return a[1] < b[1];
});
For a problem from leetcode, Method 1 caused a "Time Limit Exceeded" verdict, while Method 2 was accepted.
Can there be that much contrast between these two methods in terms of time complexity?