The same algorithm got passed with C++ but TLE with Ruby


  • 0
    F
    # @param {String[]} words
    # @return {Integer}
    def max_product(words)
        # Algorithm: Bit Maniputation  Time = O(n^2)
        trans_array = words.map { |s|
            int_32 = 0
            s.each_codepoint {|c| int_32 |= (1 << (c - 'a'.ord))}
            int_32
        }
        max_prod = 0
        for i in 0..(trans_array.size - 1)
            for j in (i + 1)..(trans_array.size - 1)
                if (trans_array[i] & trans_array[j]).zero?
                    max_prod = [max_prod, words[i].length * words[j].length].max
                end
            end
        end
        max_prod
    end

  • 0

    Try it without [...].max.


Log in to reply
 

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