16ms, Beats 100% c++ code


  • 0
    F
    vector<Interval> merge(vector<Interval>& intervals) {
        vector<Interval> res;
        if (intervals.empty()) return res;
        sort(intervals.begin(), intervals.end(), comp);
        Interval last;
        for (int i = 0; i < intervals.size(); i++) {
            if (i == 0) {
                last = intervals[i];
            } else if (intervals[i].start > last.end) {
                res.push_back(last);
                last = intervals[i];
            } else {
                last.end = max(intervals[i].end, last.end);
            }
        }
        res.push_back(last);
        return res;
    }
    static bool comp(const Interval& a, const Interval& b) {
        return (a.start < b.start);
    }
    

    };


Log in to reply
 

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