LeetCode 435. Non-overlapping Intervals

435. Non-overlapping Intervals

Description Submission Solutions Add to List

  • Total Accepted: 7406
  • Total Submissions: 18525
  • Difficulty: Medium
  • Contributors: love_FDU_llp

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.

Subscribe to see which companies asked this question.

【题目分析】

这个题目与《算法导论》中活动安排的题目非常类似。

活动选择问题
有n个需要在同一天使用同一个教室的活动a1,a2,…,an,教室同一时刻只能由一个活动使用。每个活动ai都有一个开始时间si和结束时间fi 。一旦被选择后,活动ai就占据半开时间区间[si,fi)。如果[si,fi]和[sj,fj]互不重叠,ai和aj两个活动就可以被安排在这一天。该问题就是要安排这些活动使得尽量多的活动能不冲突的举行。例如下图所示的活动集合S,其中各项活动按照结束时间单调递增排序。

考虑使用贪心算法的解法。为了方便,我们用不同颜色的线条代表每个活动,线条的长度就是活动所占据的时间段,蓝色的线条表示我们已经选择的活动;红色的线条表示我们没有选择的活动。
如果我们每次都选择开始时间最早的活动,不能得到最优解:

如果我们每次都选择持续时间最短的活动,不能得到最优解:

可以用数学归纳法证明,我们的贪心策略应该是每次选取结束时间最早的活动。直观上也很好理解,按这种方法选择相容活动为未安排活动留下尽可能多的时间。这也是把各项活动按照结束时间单调递增排序的原因。

【思路】

参照上面活动安排的例子,我们很容易得到这个题目的解法。这是一个贪心问题,我们每次都找到那个结束点最小的区间,然后依次向后找那些与前面区间不冲突且结束点早的区间。这个过程中我们把局部的最优解合并成了全局的最优解。

【java代码】

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public int eraseOverlapIntervals(Interval[] intervals) {
12         if(intervals.length == 0) return 0;
13
14         Comparator<Interval> comp = new Comparator<Interval>() {
15             public int compare(Interval interval1, Interval interval2) {
16                 if(interval1.end > interval2.end) return 1;
17                 else if(interval1.end < interval2.end) return -1;
18                 else return 0;
19             }
20         };
21
22         Arrays.sort(intervals, comp);
23         int lastend = intervals[0].end;
24         int remove = 0;
25         for(int i = 1; i < intervals.length; i++) {
26             if(intervals[i].end == lastend) remove++;
27             else if(intervals[i].start < lastend) remove++;
28             else lastend = intervals[i].end;
29         }
30
31         return remove;
32     }
33 }
时间: 2024-10-09 21:32:02

LeetCode 435. Non-overlapping Intervals的相关文章

[Leetcode][Python]56: Merge Intervals

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 56: Merge Intervalshttps://oj.leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6]

【一天一道LeetCode】#56. Merge Intervals

一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. (二)解题 /* 本题的解题步骤: 1.对vector里面的每个Interval结构体按照start的值升序排列 2.遍历整个intervals,如果有重复区域就合并,如果没

leetcode || 56、 Merge Intervals

problem: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Hide Tags Array Sort 题意:给定数组区间,合并有覆盖或者相邻的区间 thinking: (1)一開始我想到用hash table的方法,开一个总区间跨度的数组.对于有区间覆盖的数

LeetCode 56. 56. Merge Intervals 20170508

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题目大意:给出一个区间的集合,将重叠的区间合并 解题思路: 1.将集合内的区间按照区间的起始位置的大小从小到大排序 2.用一个新的list来保存合并后的结果,先放入起始位置最小的区间 3.接下来每次取原集合中起始位置最小

【leetcode】56. Merge Intervals

@requires_authorization @create_time 2015.7.10 11:07 @author johnsondu @url https://leetcode.com/problems/merge-intervals/ /** * 进行数组排序,然后按照贪心策略进行合并,具体是考察 * 下一个元素的start是否是介于当前的start和end之间, * 如果是,则根据需要更新当前end. * 时间复杂度(O(nlogn))-->排序, 结果获取O(n) * 空间复杂度(

[LeetCode]题解(python):056-Merge Intervals

题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题意分析 Input:a list of intervals Output:a list of intervals

LeetCode --- 56. Merge Intervals

题目链接:Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

LeetCode: Merge Intervals 解题报告

Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. SOLUTION 1: 1. 先使用Comparator 的匿名类对intervels进行排序. 2. 把Intervals遍历一次,依次一个一个merge到第1个interval. 把第1

LeetCode: Merge Intervals [055]

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].