How can I make it faster on ruby?


  • 0
    B

    Output is what it expected but always show me "Time Limit Exceeded". I wonder my code might pass the judge.

    class MedianFinder
    
    =begin
        initialize your data structure here.
    =end
        def initialize()
            @array = []
        end
    
    
    =begin
        :type num: Integer
        :rtype: Void
    =end
        def add_num(num)
           @array << num unless num.nil?
        end
    
    
    =begin
        :rtype: Float
    =end
        def find_median()
            @array.sort!
            size = @array.size
            half = size/2
           if size.even?
               return (@array[half] + @array[half - 1]).to_f/2
           else
               return @array[half]
           end
        end
    
    end
    
    

Log in to reply
 

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