If(<=) TLE; if(<) Accepted Why?


  • 0
    S

    I'm confused about the cmp function : when I'm using return a.start <= b.start I got a TLE, but when I use
    return a.start < b.start it can be Accepted. why ?

    class Solution {
    public:
        static bool cmp(Interval a, Interval b) {
            return a.start < b.start;
        }
        vector<Interval> merge(vector<Interval>& intervals) {
            int size = intervals.size();
            if(size < 2) return intervals;
    
            sort(intervals.begin(), intervals.end(), cmp);
            vector<Interval> res;
            int curS = intervals[0].start, curE = intervals[0].end;
    
            for(int i = 1; i < size; i++) {
                if(intervals[i].start <= curE) {
                    curE = max(curE, intervals[i].end);
                }else {
                    res.push_back(Interval(curS, curE));
                    curS = intervals[i].start;
                    curE = intervals[i].end;
                }
            }
    
            res.push_back(Interval(curS, curE));
            return res;
        }
    };

Log in to reply
 

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