【力扣 - 合并区间】

题目描述

以数组 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
}

相关推荐

  1. 56. 合并区间

    2024-03-13 17:44:06       46 阅读
  2. 56.合并区间

    2024-03-13 17:44:06       58 阅读
  3. 56. 合并区间

    2024-03-13 17:44:06       33 阅读
  4. 56.合并区间

    2024-03-13 17:44:06       43 阅读
  5. 100】56.合并区间

    2024-03-13 17:44:06       70 阅读
  6. :56. 合并区间(贪心)

    2024-03-13 17:44:06       55 阅读
  7. 【算法详解】56.合并区间

    2024-03-13 17:44:06       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-13 17:44:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-13 17:44:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-13 17:44:06       82 阅读
  4. Python语言-面向对象

    2024-03-13 17:44:06       91 阅读

热门阅读

  1. Chapter 8 - 24. Congestion Management in TCP Storage Networks

    2024-03-13 17:44:06       40 阅读
  2. 小白如何快速入门计算机视觉?

    2024-03-13 17:44:06       39 阅读
  3. vue-router

    2024-03-13 17:44:06       33 阅读
  4. 牛客网KY156 百鸡问题

    2024-03-13 17:44:06       50 阅读
  5. 按键顺序读写yaml文件

    2024-03-13 17:44:06       37 阅读
  6. 贪心算法: 奶牛做题

    2024-03-13 17:44:06       37 阅读
  7. 鸿蒙开发-UI-动画-弹簧曲线动画

    2024-03-13 17:44:06       49 阅读
  8. 题目 2701: 取模

    2024-03-13 17:44:06       45 阅读
  9. 2024年AI辅助研发:科技创新的引领者

    2024-03-13 17:44:06       43 阅读
  10. C#面:简单介绍枚举

    2024-03-13 17:44:06       47 阅读
  11. 3月12日做题总结(C/C++真题)

    2024-03-13 17:44:06       41 阅读