***Leetcode 435. Non-overlapping Intervals
阿新 • • 發佈:2018-12-13
很不錯的題 跟課程規劃那個題有點相似
bool cmp( const Interval& a , const Interval& b ) { if (a.start != b.start) return a.start < b.start; return a.end < b.end; } bool judge( Interval&a, Interval&b ) { if (a.end <= b.start || a.start >= b.end) return true; else return false; } class Solution { public: int eraseOverlapIntervals(vector<Interval>& intervals) { if (intervals.size()<=1) return 0; sort( intervals.begin(), intervals.end(), cmp ); Interval last = intervals[0]; int ret = 0; for (int i = 1; i < intervals.size(); i++) { if (!judge(last, intervals[i])) { ret ++; if (last.end > intervals[i].end) last = intervals[i]; } else { last = intervals[i]; } } return ret; } };