[LeetCode] 435. Non-overlapping Intervals Non-overlapping Intervals (Medium) (JAVA) One question per day

【LeetCode】435. Non-overlapping Intervals (Medium) (JAVA)

Subject address: https://leetcode.com/problems/non-overlapping-intervals/

Title description:

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.

General idea

Given a set of intervals, find the minimum number of intervals that need to be removed so that the remaining intervals do not overlap each other.

note:

  1. It can be considered that the end of an interval is always greater than its starting point.
  2. The boundaries of the intervals [1,2] and [2,3] "touch" each other, but do not overlap each other.

Problem-solving method

  1. This question is sorted first. After sorting, delete the overlapping elements behind and before, and then count the number of deleted elements
  2. The focus is on how to sort, such as: [[1,2],[2,3],[3,4],[1,3]], how do you choose to delete elements?
  3. If you want to delete as few elements as possible, you need to delete the elements with a large span. How to find the elements with a large span? 1. The beginning is the same, but the end is larger, the span is longer; 2. The end is the same, and the beginning is smaller, the span is longer
  4. The first way of sorting is easy to cite the opposite. For example: [[1,9],[2,3],[3,4],[4,5]], where [1,9] can only delete all in front The latter element, definitely not
  5. Use the second method: the end is the same, and the beginning is smaller, the span is longer. Sort according to the end, the end is the same, and the beginning is the smallest [[1,9],[2,3],[3,4],[4,5]] --> [[2,3], [3,4],[4,5],[1,9]]
class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> (a[1] - b[1] == 0 ? -a[0] + b[0] : a[1] - b[1]));
        int res = 0;
        int index = 0;
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] < intervals[index][1]) {
                res++;
            } else {
                index = i;
            }
        }
        return res;
    }
}

Execution time: 4 ms, defeating 51.09% of Java users
Memory consumption: 38.4 MB, defeating 59.37% of Java users

Welcome to pay attention to my official account, LeetCode updates one question every day

Guess you like

Origin blog.csdn.net/qq_16927853/article/details/112003597