This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PriorityQueue | |
def initialize | |
@heap = [] | |
end | |
def push(item, priority) | |
@heap << [item, priority] | |
bubble_up_iterative(@heap.size - 1) | |
self | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UnionFind | |
def initialize(n) | |
@d = Array.new(n, -1) | |
end | |
def root(x) | |
return x if @d[x] < 0 | |
@d[x] = root(@d[x]) | |
end |