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