Using set but memory limit exceeded, why???


  • 0
    D

    It gives the right answer in running.

    struct classcomp {
     bool operator() (const Interval& lhs, const Interval& rhs) const
     {return lhs.start<rhs.start;}
    };
    
    class SummaryRanges {
    public:
       /** Initialize your data structure here. */
      
       set<Interval,classcomp> intervals;
     
       SummaryRanges() {
           last.start=INT_MIN;
           last.end=INT_MIN;
           
           
           
       }
      
       
       Interval last;
       void addNum(int val) {
           Interval addone(val,val);
           
           if(val>(last.end))
           {
               if(val==last.end+1)
               {
                   intervals.erase(last);
                   addone.start=last.start;
                   intervals.insert(addone);
                   last=addone;
               }
               else
               {
               intervals.insert(addone);
               last=addone;
               }
            
           }
           else
           
           {
               
               auto l=intervals.lower_bound(addone);
               auto h=intervals.upper_bound(addone);
               l--;
               
               
               
            
               
               if(l==intervals.end())
               {
                   
                   intervals.insert(addone);
                   last=addone;
               }
               else if(h==intervals.end())
               {
                 
                   intervals.insert(addone);
               }
               else if((l->start)<=val&&(l->end)>=val)
               {
                 
               }
               
               else if((l->start)<=val&&(l->end)==val-1)
               {
                   
                   addone.start=l->start;
                   addone.end=val;
                   intervals.erase(l);
                   intervals.insert(addone);
               }
               else if((l->start)<val&&(l->end)<val&&(h->start)==val+1)
               {
                   
                   addone.start=val;
                   addone.end=h->end;
                   intervals.erase(h);
                   intervals.insert(addone);
               
                   
                   
                   
               }
               
               
               
               
               else if((l->start)<val&&(l->end)<val&&(h->start)>val)
               {
                   intervals.insert(addone);
               }
               
           }
       }
       
       vector<Interval> getIntervals() {
            return vector<Interval>(intervals.begin(),intervals.end());
       }
    };
    

Log in to reply
 

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