Can somebody see why my code runs out time? Thanks.


  • 0
    V
    bool operator<(Interval const & a, Interval const & b){
        return a.start < b.start;     
    }
     
    class Solution {
    public:
        vector<Interval> merge(vector<Interval> &intervals) {
            
            if(intervals.size() == 0)
                return intervals;
            
            int i;
            Interval new_interval;
            //mysort(intervals);
            sort(intervals.begin(), intervals.end());
            i = intervals.size()-1;
            
    
                
            while(i > 0){
                if(intervals[i].start <= intervals[i-1].end){
                    new_interval.start = intervals[i-1].start;
                    new_interval.end = max(intervals[i].end, intervals[i-1].end);
                    intervals.erase(intervals.begin() + i - 1, intervals.begin() + i);
                    intervals.insert(intervals.begin() + i - 1, new_interval);
                    i = i - 2;
                }
                else
                    i = i - 1;
            }
            return intervals;
        }
    };

Log in to reply
 

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