vault backup: 2022-09-04 13:55:28
This commit is contained in:
parent
543285d87b
commit
64455a46e4
|
@ -70,5 +70,33 @@ first sort the intervals.
|
||||||
pick the intervals with smallest end, which will allow us to
|
pick the intervals with smallest end, which will allow us to
|
||||||
make more room for following ones.
|
make more room for following ones.
|
||||||
|
|
||||||
|
#tip: Use `&` to reference vectors in comp.function to save
|
||||||
|
time.
|
||||||
|
|
||||||
### Solution
|
### 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
0
OJ notes/pages/Untitled.md
Normal file
0
OJ notes/pages/Untitled.md
Normal file
Loading…
Reference in a new issue