"Math" "sort" with the fewest points, accessing all the intervals in the array

Source: Internet
Author: User

Title: EPI 13.12


My code is slightly different from the code in the book and is inspired by topic 13.11. The method is to sort the array a first, then use a variable cur to record the intersection of the currently traversed interval, cur initialize to a[0], and iterate through the array starting from a[1], and update a[i if the currently traversed element Cur] has an intersection with cur; Select a point from cur to fill in the return value res, and then cur=a[i].

typedef int TIMETYPE;CLASS Interval{public:timetype left,right;interval (const timetype &a,const TimeType &b): Left (a), right (b) {}const bool operator< (const Interval &a) const{if (left!=a.left) return Left<a.left;else Return right<a.right;}}; Vector<timetype> covering (vector<interval>  A) {vector<timetype> res;if (A.empty ()) return res; Sort (A.begin (), A.end ()), Interval cur=a[0];//records the intersection of the current interval for (int i=1;i<a.size (); i++) {//There must be Cur<=a[i]if (A[i]. Left<=cur.right)//intersect {//narrow cur range int r=min (cur.right,a[i].right); cur.left=a[i].left;cur.right=r;} Else{res.push_back (cur.right); cur=a[i];} Res.push_back (cur.right); return res;}


"Math" "sort" with the fewest points, accessing all the intervals in the array

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.