More Than Concise C++ Solution


  • 0
    Z
    /**
     * Definition for an interval.
     * struct Interval {
     *     int start;
     *     int end;
     *     Interval() : start(0), end(0) {}
     *     Interval(int s, int e) : start(s), end(e) {}
     * };
     */
    #include <algorithm>
    class Solution {
    public:
        int eraseOverlapIntervals(vector<Interval>& intervals) {
            int cnt = 0;
            std::sort(intervals.begin(), intervals.end(), [](Interval &a, Interval &b){
                return a.end < b.end;
            });
            auto cur = intervals.begin();
            auto next = cur + 1;
            while (cur != intervals.end() && next != intervals.end()) {
                if (cur->end > next->start) {
                    ++cnt;
                    ++next;
                    continue;
                } else {
                    ++next;
                    cur = next - 1;
                }
            }
            return cnt;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.