Output Limit Exceeded?


  • 0
    Y
    class Solution {
    public:
        vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
            auto start = intervals.end(), end = intervals.end();
            auto insert = intervals.end();
            for(auto itr = intervals.begin(); itr != intervals.end(); ++itr) {
                if(itr->start > newInterval.end) {
                    insert = itr;
                    break;
                }
                if(itr->start <= newInterval.start && itr->end >= newInterval.start) {
                    start = itr;
                }
                if(itr->start <= newInterval.end && itr->end >= newInterval.end) {
                    end = itr;
                }
            }
            if(start == intervals.end() && end == intervals.end())
                intervals.insert(insert, newInterval);
            else if(start == intervals.end())
                end->start = newInterval.start;
            else if(end == intervals.end())
                start->end = newInterval.end;
            else if(start != end) {
                end->start = start->start;
                intervals.erase(start, end);
            }
            return intervals;
        }
    };
    

    I basically used three iterators, start to record the starting iterator for merge, end to record the ending iterator for merge, and insert to insert the newInterval if no merge will happen.

    I tested it on my computer and nothing is wrong, but I'm getting Output limit exceed error on Leetcode.


Log in to reply
 

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