Gridland Metro

  • + 0 comments

    Ruby

    def gridlandMetro(n, m, k, track)
        minMax = Hash.new(-1)
        track.each do |line|
            minMax[line[0]] = [] if minMax[line[0]] == -1            
            minMax[line[0]] << [line[1], line[2]] 
        end
        counter = 0
        minMax.values.each do |line|
            line.sort!        
            while not line.empty?
                start_point, end_point = line.shift        
                end_point = [line.shift[1], end_point].max while not line.empty? and line[0][0] <= end_point
                counter += end_point - start_point + 1
            end        
        end
        m*n - counter
    end