Output limit exceeded when submit but OK in local IDE


  • 1
    L

    The following is my solution. I have tested in local environment and it could shows out the right answer and I think there is no print in the code. But when I submit, it shows the output limit exceeded, I don't know the reason. Who could help me to find it..

    class Solution {
    public:
        vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
            vector<Interval> ans;
            bool flag = false;
            int size = intervals.size();
            for (int i = 0; i < size; i++) {
                int start = intervals[i].start,
                    end = intervals[i].end, 
                    nextStart = (i == size - 1) ? INT_MAX : intervals[i + 1].start;
                if (!flag && 
                    newInterval.start > start &&
                    newInterval.start < nextStart)
                {
                    if (newInterval.start > end)
                        ans.push_back(intervals[i]);
                    else {
                        newInterval.start = start;
                        newInterval.end = max(end, newInterval.end);
                    }
                    flag = true;
                    while (++i < size) {
                        if (intervals[i].start <= newInterval.end) {
                            if (intervals[i].end > newInterval.end)
                                newInterval.end = intervals[i].end;
                        }
                        else
                            break;
                    }
                    ans.push_back(newInterval);
                    if (i != size)
                        ans.push_back(intervals[i]);
                }
                else
                    ans.push_back(intervals[i]);
            }
            if (!flag)
                ans.push_back(newInterval);
            return ans;
        }
    };

Log in to reply
 

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