A Ruby cheat using TSort


  • 0
    A
    require 'tsort'
    
    def find_circle_num(m)
        each_node = ->(&blk) { m.size.times(&blk) }
        each_child = ->(n, &blk) { m[n].each_with_index { |x, i| blk.(i) if x == 1 } }
        TSort.strongly_connected_components(each_node, each_child).size
    end
    

Log in to reply
 

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