Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created August 16, 2020 02:08
Show Gist options
  • Save lishunan246/16df1cb1ff3a1f566bfe1bfcda029289 to your computer and use it in GitHub Desktop.
Save lishunan246/16df1cb1ff3a1f566bfe1bfcda029289 to your computer and use it in GitHub Desktop.
Non-overlapping Interval
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if (intervals.empty()) {
return 0;
}
sort(intervals.begin(), intervals.end(), [](auto&& a, auto&& b){
return a[1] == b[1] ? a[0] > b[0] : a[1] < b[1];
});
int left = 0;
int cnt = 1;
for (int i = 1; i < intervals.size(); ++i) {
auto&& l = intervals[left];
auto&& r = intervals[i];
if (l[1] > r[0]) {
continue;
}
else {
left = i;
cnt++;
}
}
return intervals.size() - cnt;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment