Skip to content

Instantly share code, notes, and snippets.

@everaldo
Created August 4, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everaldo/6e8a5ec6a89ec5890925ae6f08cb8a46 to your computer and use it in GitHub Desktop.
Save everaldo/6e8a5ec6a89ec5890925ae6f08cb8a46 to your computer and use it in GitHub Desktop.
Exercício do Hacker Ranking - me deu bastante trabalho, porque o código dá timeout. Otimizei bastante, depois deu pra simplificar
# Enter your code here. Read input from STDIN. Print output to STDOUT
class Queue
def initialize
@current_stack = []
@aux_stack = []
end
def enqueue(x)
@current_stack << x
end
def dequeue
if @aux_stack.empty?
while aux = @current_stack.pop
@aux_stack << aux
end
end
element = @aux_stack.pop
end
def peek
if @aux_stack.empty?
while aux = @current_stack.pop
@aux_stack << aux
end
end
element = @aux_stack[-1]
end
end
queue = Queue.new
res = []
q = gets.to_i
q.times do |i|
type, x = gets.split(' ').map(&:to_i)
case type
when 1 then queue.enqueue(x)
when 2 then queue.dequeue
when 3 then res << queue.peek
end
end
puts res.join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment