扫描线版本很清晰 O(nlogn) time O(n) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { map<int, int> m; for (const auto &i : intervals) { ++m[i.start]; --m[i.end]; } vector<Interval> res; int cnt = 0; int b = 0; for (const auto &p : m) { if (cnt == 0) { b = p.first; } cnt += p.second; if (cnt == 0) { res.push_back({b, p.first}); } } return res; } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { map<int, int> m; for (const auto &i : intervals) { ++m[i.start]; --m[i.end]; } int cnt = 0; int s = INT_MAX; vector<Interval> res; for (const auto &p : m) { s = min(s, p.first); cnt += p.second; if (cnt == 0) { res.emplace_back(s, p.first); s = INT_MAX; } } return res; } };
|
greedy O(nlogn) time O(1) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { sort(begin(intervals), end(intervals)); vector<vector<int>> res{intervals[0]}; for (const auto &i : intervals) { if (i[0] <= res.back()[1]) { res.back()[1] = max(res.back()[1], i[1]); } else { res.push_back(i); } } return res; } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { if (intervals.empty()) return {}; sort(begin(intervals), end(intervals)); int n = intervals.size(); vector<vector<int>> res{intervals[0]}; for (int i = 1; i < n; ++i) { if (intervals[i][0] <= res.back()[1]) { res.back()[1] = max(res.back()[1], intervals[i][1]); } else { res.push_back(intervals[i]); } } return res; } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { auto cmp = [](const Interval &lhs, const Interval &rhs) { return lhs.start < rhs.start; }; sort(intervals.begin(), intervals.end(), cmp); vector<Interval> res; for (const auto &i : intervals) { if (!res.empty() && i.start <= res.back().end) { res.back().end = max(res.back().end, i.end); } else { res.push_back(i); } } return res; } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { int n = intervals.size(); if (n == 0) return {}; sort(intervals.begin(), intervals.end(), [](const Interval &lhs, const Interval &rhs) { return lhs.start < rhs.start; }); vector<Interval> res{intervals[0]}; for (int i = 1; i < n; ++i) { if (res.back().end >= intervals[i].start) { res.back().end = max(res.back().end, intervals[i].end); } else { res.push_back(intervals[i]); } } return res; } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
class Solution { public: vector<Interval> merge(vector<Interval>& intervals) { sort(intervals.begin(), intervals.end(), [](const Interval &lhs, const Interval &rhs) { return lhs.start < rhs.start; }); int n = intervals.size(); intervals.push_back(Interval(INT_MAX, INT_MAX)); vector<Interval> res; for (int i = 0; i < n;) { int s = intervals[i].start; int e = intervals[i].end; int j = i + 1; for (; j < n && e >= intervals[j].start; ++j) { e = max(e, intervals[j].end); } res.emplace_back(s, e); i = j; } return res; } };
|