88ms Ruby solution


  • 0
    S

    The following code snippet is my Ruby solution.

    
    def insert(intervals, new_interval)
        intervals << new_interval
        intervals.sort! {|a,b| a.start <=> b.start}
        result = [intervals[0]]
        intervals.each do |i|
            if i.start >= result.last.start && i.start <= result.last.end && i.end > result.last.end
                result.last.end = i.end
            elsif i.start > result.last.end
                result << i
            end
        end
        result
    end
    
    

Log in to reply
 

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