[LeetCode]Non-overlapping Intervals

Greedy.

/**
 * 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) {
        if(intervals.size() == 0) {
            return 0;
        }

        sort(intervals.begin(), intervals.end(), [](Interval i1, Interval i2) {
            return i1.start < i2.start;
        });

        int ans = 0;

        for(int i = 0; i < intervals.size() - 1; i++) {
            if(intervals[i].end > intervals[i + 1].start) {
                ans++;
                if(intervals[i].end< intervals[i + 1].end) {
                    intervals[i + 1].end = intervals[i].end;
                }
            }
        }

        return ans;
    }
};