Created
June 27, 2014 17:00
-
-
Save GusGA/3ce055aa70b84b070541 to your computer and use it in GitHub Desktop.
the exercise's solution to the problem proposed by Moblox.io
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
| #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