【力扣 - 合并区间】

题目描述

以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [start_i, end_i] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。

示例 1:

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

示例 2:

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

提示:

1 <= intervals.length <= 10^4
intervals[i].length == 2
0 <= start_i <= end_i <= 10^4

题解:排序

思路

如果我们按照区间的左端点排序,那么在排完序的列表中,可以合并的区间一定是连续的。如下图所示,标记为蓝色、黄色和绿色的区间分别可以合并成一个大区间,它们在排完序的列表中是连续的:
在这里插入图片描述

算法

我们用数组 merged 存储最终的答案。

首先,我们将列表中的区间按照左端点升序排序。然后我们将第一个区间加入 merged 数组中,并按顺序依次考虑之后的每个区间:

如果当前区间的左端点在数组 merged 中最后一个区间的右端点之后,那么它们不会重合,我们可以直接将这个区间加入数组 merged 的末尾;

否则,它们重合,我们需要用当前区间的右端点更新数组 merged 中最后一个区间的右端点,将其置为二者的较大值。

代码

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

// Function to compare intervals for sorting
int compareIntervals(const void* a, const void* b) {
    int **arr1 = (int **)a;
    int **arr2 = (int **)b;
    // In this line, the function compares the start values of two intervals. 
    // It accesses the first element of the arrays pointed to by  arr1  and  arr2 , 
    // which corresponds to the start value of the intervals. 
    // By subtracting the start value of the second interval from the start value of the first interval, 
    // the function determines the order in which the intervals should be sorted. 
    return arr1[0][0] - arr2[0][0];
}

// Function to merge overlapping intervals
int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
    // Check if the input array is empty
    if (intervalsSize == 0) {
        *returnSize = 0;
        return NULL;
    }

    // Sort intervals based on the start value
    qsort(intervals, intervalsSize, sizeof(int*), compareIntervals);

    // Initialize variables for merged intervals
    int** merged = (int**)malloc(intervalsSize * sizeof(int*));
    *returnColumnSizes = (int*)malloc(intervalsSize * sizeof(int));
    int mergedCount = 0;

    // Iterate through the sorted intervals to merge overlapping intervals
    for (int i = 0; i < intervalsSize; ++i) {
        int L = intervals[i][0], R = intervals[i][1];
        
        // If the merged array is empty or the current interval does not overlap with the last interval in merged
        if (mergedCount == 0 || merged[mergedCount - 1][1] < L) {
            // Add the current interval to the merged array
            merged[mergedCount] = (int*)malloc(2 * sizeof(int));
            merged[mergedCount][0] = L;
            merged[mergedCount][1] = R;
            (*returnColumnSizes)[mergedCount] = 2;
            mergedCount++;
        } else {
            // Update the end of the last interval in merged if there is an overlap
            merged[mergedCount - 1][1] = (R > merged[mergedCount - 1][1]) ? R : merged[mergedCount - 1][1];
        }
    }

    *returnSize = mergedCount; // Set the size of the merged array
    return merged; // Return the merged intervals
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六月悉茗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值