LeetCode | 0435. Non-overlapping Intervals non-overlapping interval [Python]

LeetCode 0435. Non-overlapping Intervals no overlap interval Medium] [[section] [greedy] Python

Problem

LeetCode

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

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.

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.

problem

Power button

Given a collection interval, to find the minimum number of intervals to be removed, the remaining section do not overlap.

Example 1:

输入: [ [1,2], [2,3], [3,4], [1,3] ]
输出: 1
解释: 移除 [1,3] 后,剩下的区间没有重叠。

Example 2:

输入: [ [1,2], [1,2], [1,2] ]
输出: 2
解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。

Example 3:

输入: [ [1,2], [2,3] ]
输出: 0
解释: 你不需要移除任何区间,因为它们已经是无重叠的了。

note:

  1. It can be considered the end of the interval is always greater than its beginning.
  2. Interval [1,2] and [2,3] of each boundary "contacting", without overlapping each other.

Thinking

Interval greedy

Press the left end of small to large, then a pointer to temp_pos i interval, when temp_pos pointer to the right end section> i interval left point, right point and the interval temp_pos pointer> when the right end of the interval i, represents the current coverage zone i is greater than the range, it is possible to remove the current section, i retention interval, updated temp_pos pointer. As long as the right end section temp_pos pointer> i must count + left end of the interval. When the right end of the current interval <= i interval left endpoint, it represents not overlap, to update temp_pos.

Time complexity: O (len (Intervals))

Space complexity: O (. 1)

Python code

class Solution(object):
    def eraseOverlapIntervals(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: int
        """
        if not intervals or len(intervals) == 0:
            return 0
        intervals.sort(key = lambda x: x[0])  # 按左端点从小到大排序
        temp_pos = 0
        cnt = 0
        for i in range(1, len(intervals)):
            if intervals[temp_pos][1] > intervals[i][0]:  # 当当前区间右端点>i区间左端点
                if intervals[i][1] < intervals[temp_pos][1]:  # 当i区间右端点<当前区间右端点,表示i区间被覆盖在当前区间中
                    temp_pos = i  # 更新temp_pos,选择覆盖范围小的i区间
                cnt += 1  # 当前区间右端点>i区间左端点都要计数+1
            else:
                temp_pos = i  # 当当前区间右端点<=i区间左端点,表示不重叠,要更新temp_pos
        return cnt

Code address

GitHub link

Published 300 original articles · won praise 393 · Views 250,000 +

Guess you like

Origin blog.csdn.net/Wonz5130/article/details/104348616