LeetCode //C - 435. Non-overlapping Intervals

435. Non-overlapping Intervals

Given an array of intervals intervals where intervalsi = s t a r t i , e n d i start_i, end_i starti,endi, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Example 1:

Input: intervals = \[1,2,2,3,3,4,1,3]
Output: 1
Explanation: 1,3 can be removed and the rest of the intervals are non-overlapping.

Example 2:

Input: intervals = \[1,2,1,2,1,2]
Output: 2
Explanation: You need to remove two 1,2 to make the rest of the intervals non-overlapping.

Example 3:

Input: intervals = \[1,2,2,3]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.

Constraints:
  • 1 < = i n t e r v a l s . l e n g t h < = 1 0 5 1 <= intervals.length <= 10^5 1<=intervals.length<=105
  • intervalsi.length == 2
  • − 5 ∗ 1 0 4 < = s t a r t i < e n d i < = 5 ∗ 1 0 4 -5 * 10^4 <= starti < endi <= 5 * 10^4 −5∗104<=starti<endi<=5∗104

From: LeetCode

Link: 435. Non-overlapping Intervals


Solution:

Ideas:
  1. Sorting: The intervals are first sorted based on their end times using qsort and a custom comparator. Sorting by end time helps in selecting the intervals that finish the earliest, reducing the chance of future overlaps.

  2. Greedy Selection: We then iterate through the sorted intervals. The variable lastEnd keeps track of the end time of the last interval that was added to our timeline. For each interval, if its start time is less than lastEnd, it means the interval overlaps with the previous one, and we need to remove it. Otherwise, we update lastEnd to the current interval's end time.

  3. Counting Removals: The variable removeCount keeps track of the number of intervals that need to be removed. This is incremented each time we find an overlapping interval.

Caode:
c 复制代码
// Comparator function for qsort
int compare(const void* a, const void* b) {
    int* intervalA = *(int**)a;
    int* intervalB = *(int**)b;
    return intervalA[1] - intervalB[1];
}

int eraseOverlapIntervals(int** intervals, int intervalsSize, int* intervalsColSize) {
    // Sort the intervals based on their end times
    qsort(intervals, intervalsSize, sizeof(int*), compare);
    
    int removeCount = 0; // Count of intervals to remove
    int lastEnd = intervals[0][1]; // End time of the last interval considered in the timeline

    // Iterate through the intervals starting from the second one
    for (int i = 1; i < intervalsSize; i++) {
        // If the current interval starts before the last one ends, it overlaps
        if (intervals[i][0] < lastEnd) {
            removeCount++; // Need to remove an interval
        } else {
            // No overlap, update the end time to the current interval's end
            lastEnd = intervals[i][1];
        }
    }
    
    return removeCount; // Number of intervals that need to be removed
}
相关推荐
可编程芯片开发4 小时前
基于MATLAB的PEF湍流风场生成器模拟与仿真
算法
罗西的思考5 小时前
【Agentic RL / 强化学习 / OPD】OpenClaw-RL 源码阅读笔记 --- (9)--- Reward Judging
人工智能·算法·机器学习
Mr.HeBoYan6 小时前
一次持续三天才出现的丢包故障——深入解析 DPDK Memory Ordering、rte_ring 与 CPU Memory Barrier (下)
linux·网络·算法·架构·dpdk
wabs6667 小时前
关于图论【深度优先搜索的理论基础】
算法·深度优先·图论
先吃饱再说7 小时前
LeetCode 101. 对称二叉树
算法
是Dream呀8 小时前
人眼看不出的伪造,AI如何识别?实测合合信息跨模态鉴伪与去反光技术
算法
Discipline~Hai8 小时前
ARM01-ARM体系架构
linux·c语言·arm开发·架构
QN1幻化引擎8 小时前
SFA 信号场注意力:用8KB参数换248x KV Cache压缩,边缘设备也能跑长序列
人工智能·算法·gitee·gitlab
段一凡-华北理工大学8 小时前
向量数据库实战:选型、调优与落地~系列文章03:向量相似度算法全解:余弦、欧氏、内积,到底该用哪个?
大数据·数据库·人工智能·算法·机器学习·向量相似度·高炉炼铁
cjr_xyi10 小时前
AT1202Contest_c binarydigit 题解
c语言·c++·算法