Skip to content

Instantly share code, notes, and snippets.

@GusGA
Created June 27, 2014 17:00
Show Gist options
  • Save GusGA/3ce055aa70b84b070541 to your computer and use it in GitHub Desktop.
Save GusGA/3ce055aa70b84b070541 to your computer and use it in GitHub Desktop.
the exercise's solution to the problem proposed by Moblox.io
#Implement in constant time, i.e. O(1), the following operations: push, pop, retrieve the minimum element.
class Stack
attr_accessor :queue
def initialize
@queue = []
@min = []
end
def push e
@queue.push(e)
if [email protected]? && @min.min > e
@min.shift
end
@min.push e
self.queue
end
def pop
if @queue.first == @min.first
@min.shift
end
@queue.shift
end
def min
@min.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment