Code executing but performance bad: Can any one suggest improvements please.


  • 0
    V
     public   List<Interval> merge(List<Interval> intervals) { 
       
      
          List<Interval>  result = new LinkedList<Interval>();
             
             if(intervals.size() <= 1)
             
                return intervals;
                
          intervals.sort((h1,h2)->h1.start-h2.start);
       
                for(int i = 0 ; i< intervals.size(); i++){
                 
                 int currStart =  intervals.get(i).start;
                 int currEnd =    intervals.get(i).end;
               
               
                   
                      for(int j = i+1;i!=intervals.size()-1 && j < intervals.size() ;j++) {
                        
                       
                         int nextStart = intervals.get(j).start;
                         
                         if(nextStart <= currEnd){
                          
                             currEnd =  Math.max(intervals.get(j).end,currEnd);
                             i=j;
                            }
                            
                        
                        
                     
                    
                     }
               
                    
                   
                    Interval curr = new Interval(currStart,currEnd);
                    result.add(curr);
               
                  
                 
             }
            
            return result;
        }

Log in to reply
 

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