144ms c++ solution- Please help me improving the solution


  • 0
    M
    class Solution {
    public:
    	struct compare
    	{
    		bool operator()(const Interval &i1, const Interval &i2)
    		{
    			if(i1.start == i2.start)
    				return i1.end<i2.end;
    			return i1.start<i2.start;
    		}
    	};
        vector<Interval> merge(vector<Interval> &intervals) {
        	if(intervals.size()<=1)
        		return intervals;
            sort(intervals.begin(),intervals.end(), compare());
            for(int i=0;i<intervals.size();i++)
    		{
    			cout<<intervals[i].start<<intervals[i].end<<endl;
    		}
            vector<Interval> res;
            Interval curr(intervals[0].start, intervals[0].end);
            for(int i=1;i<intervals.size();i++)
            {
            	if(intervals[i].start > curr.end)
            	{
            		if(i==intervals.size()-1) {
            			res.push_back(curr);
            			res.push_back(intervals[i]);
            		}
            		else {
            			res.push_back(curr);
            			curr.start = intervals[i].start;
            			curr.end = intervals[i].end;
            		}
            	} else {
            		curr.end = max(curr.end, intervals[i].end);
            		if(i==intervals.size()-1)
            			res.push_back(curr);
            	}
            }
            return res;
        }
    };

Log in to reply
 

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