vault backup: 2022-09-04 13:55:28

This commit is contained in:
juan 2022-09-04 13:55:28 +08:00
parent 543285d87b
commit 64455a46e4
2 changed files with 28 additions and 0 deletions

View file

@ -70,5 +70,33 @@ first sort the intervals.
pick the intervals with smallest end, which will allow us to
make more room for following ones.
#tip: Use `&` to reference vectors in comp.function to save
time.
### Solution
```cpp
class Solution {
static bool comp(vector<int> &a, vector<int> &b) { return a[1] < b[1]; }
public:
int eraseOverlapIntervals(vector<vector<int>> &intervals) {
sort(intervals.begin(), intervals.end(), comp);
// Using this var to skip overlapping intervals.
auto before = intervals[0];
int ans = 0;
for (int i = 1, size = intervals.size(); i < size; i++) {
if (intervals[i][0] < before[1]) {
ans++;
} else {
before = intervals[i];
}
}
return ans;
}
};

View file