Leetcode 435. non-overlapping intervals

Source: Internet
Author: User

Tag: Ref input This note HTTP length time moved compare

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 are always bigger than their start point.
    2. intervals like [to] and [2,3] has borders "touching" but they don ' t overlap each other.

Example 1:

Input: [[up], [2,3], [3,4], [1,3]]output:1explanation: [1,3] can be removed and the rest of intervals is Non-overlapp Ing.

Example 2:

Input: [[up], [up], [up]]output:2explanation:you need to remove, and the rest of intervals Non-overlap Ping.

Example 3:

Input: [[up], [2,3]]output:0explanation:you don ' t need to remove any of the intervals since they ' re already non-overl Apping.

Subscribe to see which companies asked this question.

"Problem Analysis"

The topic is very similar to the one in the introduction to algorithms.

Activity selection Issues
There are n activities that need to use the same classroom on the same day A1,a2,..., an, the classroom can only be used by one activity at a time. Each active Ai has a start time Si and an end time of fi. Once selected, the active AI occupies the half-open time interval [Si,fi]. If [Si,fi] and [sj,fj] do not overlap, AI and AJ two activities can be scheduled on this day. The problem is to arrange these activities so that as many activities as possible can be held in a non-conflicting way. For example, the activity set S is shown, where each activity is ordered monotonically by the end time.

Consider the method of using greedy algorithm. For convenience, we use lines of different colors to represent each activity, the length of the line is the time period occupied by the activity, the blue line represents the activity we have chosen, and the red line indicates that we have no selected activity.
If we choose the earliest activity of the start time each time, we cannot get the optimal solution:

If we choose the activity with the shortest duration, we cannot get the optimal solution:

It can be proved by mathematical induction that our greedy strategy should be the earliest activity at the end time of each selection. Intuitively, it is also well understood that the selection of compatible activities in this way leaves as much time as possible for the non-scheduled activities. This is also the reason for the monotonous increment of activities by the end time.

Ideas

We can easily get the solution to this problem by referring to the example of the above activity arrangement. This is a greedy problem, we find the minimum interval of the end point each time, then we look backwards to the interval that is not in conflict with the previous interval and the ending point. In this process, we combine the local optimal solution into the global optimal solution.

"Java Code"

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  */Ten  Public classSolution { One      Public interaseoverlapintervals (interval[] intervals) { A         if(Intervals.length = = 0)return0; -          -Comparator<interval> Comp =NewComparator<interval>() { the              Public intCompare (Interval interval1, Interval interval2) { -                 if(Interval1.end > Interval2.end)return1; -                 Else if(Interval1.end < Interval2.end)return-1; -                 Else return0; +             } -         }; +          A Arrays.sort (intervals, comp); at         intLastend = Intervals[0].end; -         intremove = 0; -          for(inti = 1; i < intervals.length; i++) { -             if(Intervals[i].end = = lastend) remove++; -             Else if(Intervals[i].start < Lastend) remove++; -             ElseLastend =Intervals[i].end; in         } -          to         returnremove; +     } -}

Leetcode 435. non-overlapping intervals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.