I am new to cpp I just saw new way of writing comparator for sorting using this[]
so what is the use this here because when i was defining the comparator in
normal way ,i was getting the error like this
Edit: error
/code/Solution.cpp:22:52: error: invalid use of non-static member function
sort(intervals.begin(),intervals.end(),comp);
bool comp(vector<int>&v1, vector<int>&v2) {
return v1[1] < v2[1];
}
Original and legit way here
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) {
return a[1] < b[1];
});
int prev = INT_MIN;
int ans = 0;
for (auto &it : intervals) {
if (it[0] >= prev) {
prev = it[1];
}
else
++ans;
}
return ans;
}
};