Runtime error for a partular case, but can work for some simple cases.


  • 0
    W
    /**
    
    • Definition for an interval.
    • struct Interval {
    • int start;
      
    • int end;
      
    • Interval() : start(0), end(0) {}
      
    • Interval(int s, int e) : start(s), end(e) {}
      
    • };
      */

    int cmp(const Interval &a, const Interval &b)
    {
    /*
    if(a.start < b.start)
    return 1;
    else if(a.start == b.start)
    {
    if(a.end <= b.end)
    return 1;
    else
    return 0;
    }
    else
    return 0;
    */
    if(a.start <= b.start)
    return 1;
    else
    return 0;
    }

    class Solution {
    public:
    vector<Interval> merge(vector<Interval> &intervals)
    {
    int i;
    vector<Interval> result;
    if(intervals.size() == 0)
    return result;
    sort(intervals.begin(), intervals.end(), cmp);

    Interval tmp = intervals[0];
    vector<Interval>::iterator iter;
    for(iter = intervals.begin() + 1; iter !=intervals.end(); iter++)
    {
    	if(tmp.end >= iter -> start)
    	{
    		if(tmp.end <= iter -> end)
    			tmp.end = iter -> end;
    		else
    			tmp = tmp;
    		if(iter == intervals.end() - 1)
    			result.push_back(tmp);
    	}
    	else
    	{
    		result.push_back(tmp);
    		tmp = *iter;
    		if(iter == intervals.end() - 1)
    			result.push_back(*iter);
    	}
    }
    
    return result;
    

    }

    };

    runtime error
    case
    [[74,78],[61,63],[46,50],[51,54],[50,50],[60,64],[39,42],[25,27],[91,95],[14,16],[85,85],[5,7],[45,46],[45,49],[66,66],[73,73],[25,26],[25,26],[45,48],[67,67],[63,65],[82,84],[90,92],[47,49],[3,4],[1,5],[64,66],[73,77],[90,94],[20,21],[84,87],[48,49],[80,80],[85,85],[53,55],[21,23],[31,34],[71,75],[62,65],[8,9],[32,33],[7,8],[20,22],[57,57],[51,53],[18,21],[71,75],[49,50],[44,45],[28,31],[38,42],[15,17],[4,7],[48,52],[9,10],[37,38],[69,70],[89,89],[28,29],[52,52],[75,76],[20,21],[90,94],[49,53],[56,59],[84,85],.....[40,40],


Log in to reply
 

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