Sixth week greedy algorithm in algorithm class

Source: Internet
Author: User
Tags sort
given a collection of intervals, find the minimum number of intervals you need to remove

The rest of the intervals non-overlapping.
Note:you may assume the interval's end point are always bigger than their start point.
intervals like [to] and [2,3] has borders "touching" but they don ' t overlap each other. Example 1:input: [[+], [2,3], [3,4], [1,3]] output:1 explanation: [1,3] can be removed and the rest of intervals a
Re non-overlapping. Example 2:input: [[[] [[], [[]], [+]] output:2 explanation:you need to remove both [from] to make the rest of Interv
ALS non-overlapping. Example 3:input: [[+], [2,3]] output:0 explanation:you don ' t need to remove any of the intervals since they ' re al
Ready non-overlapping. Subscribe to see which companies asked this question. 

This problem gives us a bunch of intervals, so we need to remove at least how many intervals to make the rest of the interval does not overlap, then we first want to sort the interval, based on the start of each interval to do ascending order, and then we start to look for overlapping intervals, Judging method is to see if the previous interval of the end is greater than the next interval of start, then must be a repeating interval, at this time we have the result res since 1, we need to delete one, then we should remove which one, in order to ensure that we remove the total interval number of the smallest, we remove the larger end value of the interval , and in the code, we did not really delete a certain interval, but with a variable last point to the interval to be compared, we will point to the end of the lower value of the interval, if the two interval does not overlap, then the last point to the current interval, continue the next traversal, See the code below:

/** * Definition for an interval.
 * struct Interval {* int start;
 * int end;
 * Interval (): Start (0), end (0) {} * Interval (int s, int e): Start (s), End (e) {} *}; */class Solution {Public:int eraseoverlapintervals (vector<interval>& intervals) {int Res=0,n=int
        Ervals.size (), last=0;
        Auto Comp = [] (const interval& i1, const interval& i2) {return I1.start < i2.start;};
        Sort (Intervals.begin (), Intervals.end (), comp);
                for (int i=1;i<n;i++) {if (intervals[i].start<intervals[last].end) {++res;
            if (intervals[i].end<intervals[last].end) last=i;
            }else{last=i;
    }} return res; }
};
Related Article

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.