Created
August 16, 2020 02:08
-
-
Save lishunan246/16df1cb1ff3a1f566bfe1bfcda029289 to your computer and use it in GitHub Desktop.
Non-overlapping Interval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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