LeetCode //C - 56. Merge Intervals

这篇具有很好参考价值的文章主要介绍了LeetCode //C - 56. Merge Intervals。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

56. Merge Intervals

Given an array of intervals where intervals[i] = [ s t a r t i , e n d i ] [start_i, end_i] [starti,endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
 

Example 1:

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].

Example 2:

Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.

Constraints:
  • 1 < = i n t e r v a l s . l e n g t h < = 1 0 4 1 <= intervals.length <= 10^4 1<=intervals.length<=104
  • intervals[i].length == 2
  • 0 < = s t a r t i < = e n d i < = 1 0 4 0 <= start_i <= end_i <= 10^4 0<=starti<=endi<=104

From: LeetCode
Link: 56. Merge Intervals


Solution:

Ideas:

To merge overlapping intervals:

  1. First, we sort the intervals based on the starting times.
  2. Then, we start with the first interval and compare its end with the start of the next interval.
  3. If they overlap (i.e., the end of the current interval is greater than or equal to the start of the next interval), we merge them.
  4. If they don’t overlap, we simply move to the next interval.
  5. Continue this process until all intervals are processed.

This code first sorts the intervals by their start times. Then, it goes through each interval, checking if it can be merged with the previous one. If it can, it updates the end time of the merged interval. If not, it adds a new interval to the merged list.

The function returns the merged intervals and updates the returnSize and returnColumnSizes accordingly.文章来源地址https://www.toymoban.com/news/detail-652578.html

Code:
/**
 * 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().
 */
int compare(const void* a, const void* b) {
    return (*(int**)a)[0] - (*(int**)b)[0];
}

int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
    if (intervalsSize == 0) {
        *returnSize = 0;
        return NULL;
    }

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

    // Create an array to store merged intervals
    int** merged = (int**)malloc(intervalsSize * sizeof(int*));
    int mergedSize = 0;

    for (int i = 0; i < intervalsSize; i++) {
        // If merged array is empty or current interval does not overlap with previous, just add to merged array
        if (mergedSize == 0 || merged[mergedSize - 1][1] < intervals[i][0]) {
            merged[mergedSize] = (int*)malloc(2 * sizeof(int));
            merged[mergedSize][0] = intervals[i][0];
            merged[mergedSize][1] = intervals[i][1];
            mergedSize++;
        } else {
            // Merge the overlapping intervals
            if (merged[mergedSize - 1][1] < intervals[i][1]) {
                merged[mergedSize - 1][1] = intervals[i][1];
            }
        }
    }

    // Set the returnSize and returnColumnSizes
    *returnSize = mergedSize;
    *returnColumnSizes = (int*)malloc(mergedSize * sizeof(int));
    for (int i = 0; i < mergedSize; i++) {
        (*returnColumnSizes)[i] = 2; // Since each interval consists of 2 integers
    }

    return merged;
}

到了这里,关于LeetCode //C - 56. Merge Intervals的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【LeetCode题目详解】第八章 贪心算法 part05 435. 无重叠区间 763.划分字母区间 56. 合并区间 (day36补)

    给定一个区间的集合  intervals  ,其中 intervals[i] = [starti, endi]  。返回 需要移除区间的最小数量,使剩余区间互不重叠  。 示例 1: 示例 2: 示例 3: 提示: 1 = intervals.length = 105 intervals[i].length == 2 -5 * 104 = starti  endi = 5 * 104 相信很多同学看到这道题目都冥冥之中感觉要排序,但

    2024年02月11日
    浏览(29)
  • LeetCode56.合并区间

    这道题我想了一会儿,实在想不到比较好的算法,只能硬着头皮写了,然后不断的debug,经过我不懈的努力,最后还是AC,不过效率确实低。 我就是按照最直接的方法来,先把intervals数组按照第一个数start来排序,这个是通过定义一个sort方法用冒泡排序实现的,然后用一个L

    2024年02月11日
    浏览(30)
  • Leetcode56. 合并区间

    以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。 示例 1: 输入:intervals = [[1,3],[2,6],[8,10],[15,18]] 输出:[[1,6],[8,10],[15,18]] 解释:区间 [1,3] 和

    2024年01月23日
    浏览(30)
  • LeetCode[56]合并区间

    难度:Medium 题目: 以数组  intervals  表示若干个区间的集合,其中单个区间为  intervals[i] = [starti, endi]  。请你合并所有重叠的区间,并返回  一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间  。 示例 1:   示例 2: 提示: 1 = intervals.length = 104 intervals[i].lengt

    2024年02月12日
    浏览(54)
  • LeetCode+ 56 - 60

    双指针算法、位运算、离散化、区间合并_小雪菜本菜的博客-CSDN博客

    2024年01月22日
    浏览(62)
  • 【LeetCode: 56. 合并区间+贪心+双指针+标识+模拟】

    🚀 算法题 🚀 🌲 算法刷题专栏 | 面试必备算法 | 面试高频算法 🍀 🌲 越难的东西,越要努力坚持,因为它具有很高的价值,算法就是这样✨ 🌲 作者简介:硕风和炜,CSDN-Java领域优质创作者🏆,保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文

    2024年02月10日
    浏览(28)
  • Leetcode 56. 合并区间(排序 + sort 方法的注意)

    Leetcode 56. 合并区间(排序 + sort 方法的注意) 题目 以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。 请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。 1 = intervals.length = 10^4 intervals[i].length ==

    2024年02月10日
    浏览(41)
  • LeetCode每日一题:56. 合并区间(2023.8.27 C++)

    目录 56. 合并区间 题目描述: 实现代码与解析: 排序 + 贪心 原理思路:         以数组  intervals  表示若干个区间的集合,其中单个区间为  intervals[i] = [starti, endi]  。请你合并所有重叠的区间,并返回  一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间  。

    2024年02月11日
    浏览(32)
  • LeetCode //88. Merge Sorted Array

    You are given two integer arrays nums1 and nums2 , sorted in non-decreasing order, and two integers m and n , representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1

    2024年02月12日
    浏览(38)
  • LeetCode 36天 | 435.无重叠区域 763.划分字母区间 56.合并区间

    435. 无重叠区间 左边排序,右边裁剪为当前最小的 763. 划分字母区间 自己写出来的题,虽然之前做过一遍了。 自己的写法虽然比较难看,但是也列出来了。 再给一个卡尔的写法 56. 合并区间 重叠区域的题目大都要按左边界先排序。学了个lambda表达式。可以直接将一个区域放

    2024年02月20日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包