Last active
July 2, 2019 23:49
-
-
Save hkurokawa/34d09212e54f01e5c6ae70237c38e3d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MyQueue | |
def initialize(size) | |
@size = 0 | |
@min_index = 0 | |
@queues = Array.new(size) { Array.new } | |
end | |
def push(cost, index) | |
if cost < 0 || cost >= @queues.size | |
raise "invalid cost: cost=#{cost}, size=#{@queues.size}" | |
end | |
@queues[cost] << [cost, index] | |
@size += 1 | |
end | |
def peek() | |
i = 0 | |
while i < @queues.size | |
q = @queues[i] | |
return q[0] unless q.empty? | |
i += 1 | |
end | |
nil | |
end | |
def set_min_index(index) | |
raise "Cannot set a smaller index than the current: index=#{index}, @min_index=#{@min_index}" if index < @min_index | |
@min_index = index | |
i = 0 | |
while i < @queues.size | |
q = @queues[i] | |
j = 0 | |
while j < q.size && q[j][1] < @min_index | |
j += 1 | |
end | |
q.shift(j) | |
@size -= j | |
break unless q.empty? | |
i += 1 | |
end | |
end | |
end | |
# q = MyQueue.new(11) | |
# q.push(0, 0) | |
# q.push(8, 1) | |
# q.push(0, 2) | |
# q.push(8, 3) | |
# q.push(10, 4) | |
# p q.peek | |
# q.set_min_index(1) | |
# p q.peek | |
# q.set_min_index(2) | |
# p q.peek | |
# q.set_min_index(3) | |
# p q.peek | |
# q.set_min_index(4) | |
# p q.peek |
This file contains hidden or 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 MyQueue | |
def initialize(size) | |
@size = 0 | |
@min_index = 0 | |
@min_value = nil | |
@min_value_queue = 0 | |
@queues = Array.new(size) { Array.new } | |
end | |
def push(cost, index) | |
if cost < 0 || cost >= @queues.size | |
raise "invalid cost: cost=#{cost}, size=#{@queues.size}" | |
end | |
@queues[cost] << [cost, index] | |
@size += 1 | |
if @min_value.nil? || @min_value[0] > cost | |
@min_value = @queues[cost].first | |
@min_value_queue = cost | |
end | |
end | |
def peek() | |
@min_value | |
end | |
def set_min_index(index) | |
raise "Cannot set a smaller index than the current: index=#{index}, @min_index=#{@min_index}" if index < @min_index | |
@min_index = index | |
i = @min_value_queue | |
while i < @queues.size | |
q = @queues[i] | |
j = 0 | |
while j < q.size && q[j][1] < @min_index | |
j += 1 | |
end | |
q.shift(j) | |
@size -= j | |
unless q.empty? | |
@min_value = q.first | |
@min_value_queue = i | |
return | |
end | |
i += 1 | |
end | |
@min_value = nil | |
@min_value_queue = 0 | |
end | |
end | |
# q = MyQueue.new(11) | |
# q.push(0, 0) | |
# q.push(8, 1) | |
# q.push(0, 2) | |
# q.push(8, 3) | |
# q.push(10, 4) | |
# p q.peek | |
# q.set_min_index(1) | |
# p q.peek | |
# q.set_min_index(2) | |
# p q.peek | |
# q.set_min_index(3) | |
# p q.peek | |
# q.set_min_index(4) | |
# p q.peek |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment