Why my C++ code take 608ms


  • 1
    R

    How can improve it?
    I saw a group of c++ submission that is under 50ms in chart.
    Can anyone tell me how to achieve that?
    Or guide me to the link if already posted?
    Thanks

    class Solution {
    public:
        bool static wayToSort(Interval i, Interval j)
        {
            return i.start < j.start;
        }
        
        vector<Interval> merge(vector<Interval>& intervals) {
            vector<Interval> newVec;
            if(intervals.size()==0)
                return newVec;
            
            std::sort(intervals.begin(), intervals.end(), wayToSort);
            newVec.push_back(intervals[0]);
            for(int i=1; i<intervals.size(); i++)
            {
                if(intervals[i].start<=newVec.back().end)
                    newVec.back().end = std::max(newVec.back().end, intervals[i].end);
                else
                    newVec.push_back(intervals[i]);
            }
            
            return newVec;
        }
    };

  • 0
    K

    When I review and run my code that I wrote before, the time also increases a lot. I guess the reason is Leetcode modified its test cases, so you don't need to worry.


Log in to reply
 

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