当前位置: 首页 > news >正文

LeetCode //C - 57. Insert Interval

57. Insert Interval

You are given an array of non-overlapping intervals intervals where intervals[i] = [ s t a r t i , e n d i start_i, end_i starti,endi] represent the start and the end of the i t h i^{th} ith interval and intervals is sorted in ascending order by s t a r t i start_i starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.

Insert newInterval into intervals such that intervals is still sorted in ascending order by s t a r t i start_i starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).

Return intervals after the insertion.
 

Example 1:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].

Constraints:

  • 0 < = i n t e r v a l s . l e n g t h < = 1 0 4 0 <= intervals.length <= 10^4 0<=intervals.length<=104
  • intervals[i].length == 2
  • 0 < = s t a r t i < = e n d i < = 1 0 5 0 <= start_i <= end_i <= 10^5 0<=starti<=endi<=105
  • intervals is sorted by starti in ascending order.
  • newInterval.length == 2
  • 0 < = s t a r t < = e n d < = 1 0 5 0 <= start <= end <= 10^5 0<=start<=end<=105

From: LeetCode
Link: 57. Insert Interval


Solution:

Ideas:

The main idea behind the code is to break down the insertion process into three primary segments:

  1. Before the new interval: This phase processes all intervals that come entirely before the newInterval without overlapping.
  2. Merging overlapping intervals with the new interval: During this phase, any intervals that overlap with the newInterval are merged into a single interval.
  3. After the new interval: This phase processes all intervals that come entirely after the newInterval without overlapping.

1. Before the new interval:
In this phase, the algorithm checks all intervals that are entirely before the newInterval. This is determined by checking if the end of the current interval is less than the start of the newInterval. All such intervals are directly added to the result because they won’t overlap with the newInterval.

2. Merging overlapping intervals with the new interval:
In this phase, the algorithm checks for any intervals that overlap with the newInterval. An overlap is determined if the start of the current interval is less than or equal to the end of the newInterval. For each overlapping interval, the start of the merged interval becomes the minimum of the current interval’s start and the newInterval’s start. Similarly, the end of the merged interval becomes the maximum of the current interval’s end and the newInterval’s end.

3. After the new interval:
Post merging, all intervals that come entirely after the merged newInterval are added to the result as they are, because they won’t have any overlap with the newInterval.

In the end, the function updates the returnSize to indicate the number of intervals in the result, and returnColumnSizes is updated to indicate the size of each interval (which is always 2). The result is then returned.

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** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes){// Initializationsint** res = (int**)malloc(sizeof(int*) * (intervalsSize + 1));  // +1 in case we don't merge and just insert the new intervalint* colSizes = (int*)malloc(sizeof(int) * (intervalsSize + 1));int index = 0, resIndex = 0;// Before the new intervalwhile(index < intervalsSize && intervals[index][1] < newInterval[0]){res[resIndex] = intervals[index];colSizes[resIndex] = 2;resIndex++;index++;}// Merge overlapping intervals with new intervalwhile(index < intervalsSize && intervals[index][0] <= newInterval[1]){newInterval[0] = fmin(newInterval[0], intervals[index][0]);newInterval[1] = fmax(newInterval[1], intervals[index][1]);index++;}// Add the merged new intervalint* mergedInterval = (int*)malloc(sizeof(int) * 2);mergedInterval[0] = newInterval[0];mergedInterval[1] = newInterval[1];res[resIndex] = mergedInterval;colSizes[resIndex] = 2;resIndex++;// After the new intervalwhile(index < intervalsSize){res[resIndex] = intervals[index];colSizes[resIndex] = 2;resIndex++;index++;}// Set return sizes*returnSize = resIndex;*returnColumnSizes = colSizes;return res;
}
http://www.lryc.cn/news/129041.html

相关文章:

  • android手势事件
  • [网络安全学习篇01]:windowsxp、windows2003、windows7、windows2008系统部署(千峰网络安全视频笔记)
  • CANoe自动化工程的搭建
  • 第6章:支持向量机
  • ROS机器人启动move base时代价地图概率性无法加载的原因及解决方法
  • 快速上手PyCharm指南
  • 数字图像处理 - 图像处理结合机器学习的应用示例
  • Linux命令200例:zip和unzip用于压缩和解压文件(常用)
  • 通过 HttpClient 发送请求
  • 管理类联考——逻辑——真题篇——按知识分类——汇总篇——一、形式逻辑——假言——第二节 必要条件假言+第三节 特殊假言
  • 算法笔记:A*算法
  • postgresql 分类排名
  • TCP服务器实现—多进程版,多线程版,线程池版
  • Nginx 配置文件的完整指南 (二)
  • AI夏令营第三期 - 基于论文摘要的文本分类与关键词抽取挑战赛笔记
  • 使用qsqlmysql操作mysql提示Driver not loaded
  • Java云原生框架Quarkus初探
  • ElasticSearch相关概念
  • 微服务实战项目-学成在线-项目部署
  • 封装form表单
  • 程序员如何利用公网远程访问查询本地硬盘【内网穿透】
  • 算法|Day42 动态规划10
  • vmalert集成钉钉告警
  • 深入解析 MyBatis 中的 <foreach> 标签:优雅处理批量操作与动态 SQL
  • LeGO-Loam代码解析(二)--- Lego-LOAM的地面点分离、聚类、两步优化方法
  • 程序员如何利用公网打造低成本轻量化的搜索和下载平台【内网穿透】
  • 构建可远程访问的企业内部论坛
  • 2023河南萌新联赛第(六)场:河南理工大学-C 旅游
  • C语言 常用工具型API ----------strchr()
  • 建造者模式的理论与实现