435. Non-overlapping Intervals

本文介绍了一种算法,用于找出最小数量需要移除的区间,使剩余区间不重叠。通过排序和遍历的方式,实现了一个解决方案,并给出了示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Note:

  1. You may assume the interval's end point is always bigger than its start point.
  2. Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.

 

Example 1:

Input: [ [1,2], [2,3], [3,4], [1,3] ]

Output: 1

Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.

 

Example 2:

Input: [ [1,2], [1,2], [1,2] ]

Output: 2

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

 

Example 3:

Input: [ [1,2], [2,3] ]

Output: 0

Explanation: You don't need to remove any of the intervals since they're already non-overlapping.

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public int eraseOverlapIntervals(Interval[] intervals) {
        if(intervals.length == 0) return 0;
        Arrays.sort(intervals, new Comparator<Interval>(){
            public int compare(Interval a, Interval b){
                return a.start - b.start;
            }
        });
        int res = 0;
        int min = intervals[0].end;
        for(int i = 1 ; i < intervals.length ; i++){
            if(intervals[i].start < min){
                res++;
                min = Math.min(min, intervals[i].end);  // key point to make sure minimum : del the inteval with large end
            }
            else
              min = intervals[i].end;
        }
        return res;
    }
}

 

转载于:https://www.cnblogs.com/joannacode/p/6108943.html

### 力扣435题无重叠区间问题的Python解决方案 对于力扣435题——Non-overlapping Intervals,目标是从给定的一组区间中移除尽可能少的数量使得剩余区间互不重叠。一种高效的方法是采用贪心算法来解决这个问题。 为了有效地解决问题,可以先按照区间的结束位置升序排列这些区间[^1]。通过这种方式,在遍历时总是优先保留那些较早结束的区间,从而为后续可能加入更多非重叠区间留出空间。 下面是具体的Python实现代码: ```python class Interval(object): def __init__(self, start, end): self.start = start self.end = end def eraseOverlapIntervals(intervals): if not intervals: return 0 # Sort the intervals based on their ending time. sorted_intervals = sorted(intervals, key=lambda interval: interval.end) count = 0 prev_end = float('-inf') for current_interval in sorted_intervals: if current_interval.start >= prev_end: # If there is no overlap with previous selected interval, # update `prev_end` and continue without incrementing removals counter (`count`) prev_end = current_interval.end else: # Otherwise, this means we have an overlapping pair; # since intervals are already ordered by 'end', simply increase our removal tally (`count`) count += 1 return count ``` 此方法的时间复杂度主要取决于排序操作O(n log n),其中n表示输入区间的数量;而随后的一次线性扫描只需要O(n)时间。因此整体性能表现良好,适用于大多数实际应用场景中的数据规模[^2]。 值得注意的是,上述逻辑确保了每次遇到新的候选区间时都会选择最早结束的那个作为下一个非重叠部分的一部分,这样就能最大限度地减少被删除掉的区间数目[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值