【Medium】 56-Merge Intervals

topic

Given a collection of intervals, merge all overlapping intervals.

Given a set of intervals, please merge all overlapping intervals.

Example1

输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

Example2

输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

Source: LeetCode
Link: https://leetcode.com/problems/merge-intervals/

solution

Method: Sort

Problem-solving ideas

First, we used a number axis to abstract the process of merging intervals. For a set of intervals, we can get the final interval by their coverage on the number axis, as long as the left-most covered block starts to judge its adjacent area Whether the blocks can be merged, and gradually check back to get all the final intervals

It can be known that the basis for judging whether two intervals can be merged is the relationship between the starting point of the interval of the larger starting point and the ending point of the interval of the smaller starting point (if the starting point is the same, it can be merged). Assuming that the starting point of interval a is smaller than the starting point of interval b, then if the starting point of interval b <= the ending point of interval a, then the two intervals can be merged, the starting point of the merge is the starting point of interval a, the ending point of the merger is the interval a and the interval The larger end point between b.

So for a group of intervals, as long as we merge the input intervals in sequence, for example, for intervals 1, 2, ... n, first consider interval 1 as interval a, and interval 2 as interval b, After merging, the new interval obtained continues to merge with interval 3. When the merged new interval Inter and interval i cannot be merged, the interval Inter is one of the final results; at this time, we will use i as the new interval a , I + 1 as the interval b repeat the above process.

But the premise is that the starting point of the interval 1-n is arranged from small to large. We have no way to ensure that the input is sorted according to the size of the starting point, so we must first sort the intervals, and then merge according to the above algorithm to get the final result

Code

class Solution {
public:
    static bool comp(const vector<int> &a, const vector<int> &b){
        return a[0] < b[0];
    }
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> res;
        sort(intervals.begin(), intervals.end(), comp); // 使用comp函数辅助排序
        int start = 0, end = 0;
        for(int i = 0;  i < intervals.size(); ++i){ // 从左到右排查数轴上的区块
            end = intervals[i][1];
            res.push_back(vector<int>(2, intervals[i][0])); // 新区间的起点一定是第一个区块的起点
            while(i<intervals.size()-1 && end >= intervals[i+1][0]){ // 下一个区块可以合并
                end = intervals[i+1][1] > end ? intervals[i+1][1] : end; // 判断当前区间的终点
                i++; // 继续判断下一个区块是否可以合并
            }
            res[res.size()-1][1] = end; // 后面已经没有可以合并的区块,就结束当前区间,从下一个区间开始找
        }
        return res;
    }
};

Guess you like

Origin www.cnblogs.com/suata/p/12711183.html