【两次过】Lintcode 1242. 无重叠区间

博客讨论了如何找到需要移除的最小区间数,以确保剩余区间不重叠。通过两种解题思路,包括转换问题为寻找最长不重叠子序列和使用贪心算法,按照区间结束点升序选择不重叠区间。

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

给定一些区间,找到需要移除的最小区间数,以使其余的区间不重叠。

样例

样例1:

输入: [ [1,2], [2,3], [3,4], [1,3] ]

输出: 1

解释: [1,3] 被移除后,剩下的区间将不再重叠。

样例2:

输入: [ [1,2], [1,2], [1,2] ]

输出: 2

解释: 需要将两个 [1,2] 移除使得剩下的区间不重合。

样例3:

输入: [ [1,2], [2,3] ]

输出: 0

解释: 不需要移除任何区间因为本身就没有任何区间重合。

注意事项

  1. 可以假设区间的终止点一定比起始点大。
  2. 区间[1,2]和[2,3]虽然边缘重合,但是它们并未重叠。

解题思路1:

转换题意为找到最长不重叠区间的数目,最后结果只需要list.size()-最长不重叠区间数目。

而找最长不重叠子序列,就类似Lintcode 76. 最长上升子序列,将区间按start升序,end升序,依次查找即可。

/**
 * Definition of Interval:
 * public classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 * }
 */

public class Solution {
    /**
     * @param intervals: a collection of intervals
     * @return: the minimum number of intervals you need to remove
     */
    public int eraseOverlapIntervals(List<Interval> intervals) {
        // write your code here
        //按照start升序,end升序
        Collections.sort(intervals, new Comparator<Interval>(){
            @Override
            public int compare(Interval a, Interval b){
                if(a.start != b.start)
                    return a.start - b.start;
                else
                    return a.end - b.end;
            }
        });
        
        int len = intervals.size();
        int[] dp = new int[len];
        int res = 0;
        
        for(int i=0; i<len; i++){
            dp[i] = 1;
            
            for(int j=0; j<i; j++)
                if(intervals.get(j).end <= intervals.get(i).start)
                    dp[i] = Math.max(dp[i], 1 + dp[j]);
            
            res = Math.max(res, dp[i]);
        }
        
        return len - res;
    }
}

解题思路2:

贪心算法。注意每次选择中,结尾很重要,结尾越小,留给后面的空间就越大。所以可以按照end升序排序,每次选择结尾最早,且和前一个区间不重叠的区间。

/**
 * Definition of Interval:
 * public classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 * }
 */

public class Solution {
    /**
     * @param intervals: a collection of intervals
     * @return: the minimum number of intervals you need to remove
     */
    public int eraseOverlapIntervals(List<Interval> intervals) {
        // write your code here
        //按照end升序
        Collections.sort(intervals, new Comparator<Interval>(){
            @Override
            public int compare(Interval a, Interval b){
                if(a.end != b.end)
                    return a.end - b.end;
                else
                    return a.start - b.start;
            }
        });
        
        int res = 1;    //记录最长不重叠区间数目
        int pre = 0;    //记录i之前的不重叠区间位置
        for(int i=1; i<intervals.size(); i++){
            if(intervals.get(i).start >= intervals.get(pre).end){
                res++;
                pre = i;
            }
        }
        
        return intervals.size() - res;
    }
}

 

### 力扣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、付费专栏及课程。

余额充值