From 64455a46e44c39f578bbf7826e8868c0acdb8484 Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 4 Sep 2022 13:55:28 +0800 Subject: [PATCH] vault backup: 2022-09-04 13:55:28 --- .../Leetcode Non-Overlapping-Intervals.md | 28 +++++++++++++++++++ OJ notes/pages/Untitled.md | 0 2 files changed, 28 insertions(+) create mode 100644 OJ notes/pages/Untitled.md diff --git a/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md b/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md index 5300cb8..9941e7f 100644 --- a/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md +++ b/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md @@ -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 &a, vector &b) { return a[1] < b[1]; } + +public: + int eraseOverlapIntervals(vector> &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; + } +}; diff --git a/OJ notes/pages/Untitled.md b/OJ notes/pages/Untitled.md new file mode 100644 index 0000000..e69de29