summaryrefslogtreecommitdiff
path: root/lib/quick_find.rb
blob: 419ed8b50c6e0e42074a24243455d13b656b280f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class QuickFind
  def initialize(size)
    @items = []
    size.times.each do |n|
      @items[n] = n
    end
  end

  def connected?(x, y)
    @items[x] == @items[y]
  end

  def union(x, y)
    x_value = @items[x]
    y_value = @items[y]

    @items.size.times do |n|
      if @items[n] == x_value
        @items[n] = y_value
      end
    end
  end
end