1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: int eraseOverlapIntervals(vector<vector<int>>& intervals) { if (intervals.empty()) return 0; sort(intervals.begin(), intervals.end(), [](const auto& a, const auto& b) { return a[1] < b[1]; }); const int n = intervals.size(); int right = intervals[0][1]; int remains = 1; for (int i = 1; i < n; ++i) if (intervals[i][0] >= right) { ++remains; right = intervals[i][1]; } return n - remains; } };
|