diff options
Diffstat (limited to 'other/insert_interval.cpp')
-rw-r--r-- | other/insert_interval.cpp | 39 |
1 files changed, 0 insertions, 39 deletions
diff --git a/other/insert_interval.cpp b/other/insert_interval.cpp deleted file mode 100644 index 50e16f8..0000000 --- a/other/insert_interval.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "leetcode.h" - -class Solution { -public: - vvd insert(vvd & intervals, vector<int> &newInterval) { - const int START = 0, END = 1; - int s = newInterval[START], e = newInterval[END]; - vvd left, right; - for (auto &curr : intervals) { - if (curr[END] < s) - left.push_back(curr); - else if (curr[START] > e) - right.push_back(curr); - else { - s = min(s, curr[START]); - e = max(e, curr[END]); - } - } - vvd ans; - ans.insert(ans.end(), left.begin(), left.end()); - ans.push_back({s, e}); - ans.insert(ans.end(), right.begin(), right.end()); - return ans; - } -}; - -int main() { - Solution obj; - vvd intervals1 = {{1, 3}, {6, 9}}, - intervals2 = {{1, 2}, {3, 5}, {6, 7}, {8, 10}, {12, 16}}; - vector<int> newInterval1 = {2, 5}, newInterval2 = {4, 8}; - for (auto i : obj.insert(intervals1, newInterval1)) - for (auto j : i) - cout << j << ' '; // expect: [[1,5],[6,9]] - cout << endl; - for (auto i : obj.insert(intervals2, newInterval2)) - for (auto j : i) - cout << j << ' '; // expect: [[1,2],[3,10],[12,16]] -} |