Created
August 4, 2017 23:46
-
-
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
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
# 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