题目:
代码(首刷看解析 2024年2月17日):
class Solution {
private:
const static bool cmp(vector<int>& a,vector<int>& b) {
return a[0] < b[0];
}
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if (intervals.size() < 2) return 0;
sort(intervals.begin(), intervals.end(), cmp);
int res = 0;
int preIndex = 0;
for (int i = 1; i < intervals.size(); ++i) {
if (intervals[i][0] >= intervals[preIndex][1]) preIndex = i;
else {
if (intervals[i][1] < intervals[preIndex][1]) preIndex = i;
res++;
}
}
return res;
}
};