Here's my n*log(n) solution, but it's Time Limit Exceed. Anybody knows why? Thanks!


  • 0
    J
    static bool cmp(const Interval i1, const Interval i2) {
    return i1.start <= i2.start;
    }
        
    
    vector<Interval> merge(vector<Interval>& intervals) {
    if (intervals.size() <= 1) {
        return intervals;
    }
    sort(intervals.begin(), intervals.end(), cmp);
    
    vector<Interval> res;
    res.push_back(intervals[0]);
    for (int i = 1; i < intervals.size(); ++i) {
        if (intervals[i].start > res.back().end) {
            res.push_back(intervals[i]);
            continue;
        }
        res.back().end = max(intervals[i].end, res.back().end);
    }
    return res;
    }

Log in to reply
 

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