Created
December 13, 2010 19:06
-
-
Save tal/739427 to your computer and use it in GitHub Desktop.
Stack class with built in min value
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 Stack | |
| def initialize | |
| @main = [] | |
| @min = [] | |
| end | |
| def push val | |
| @main << val | |
| @min << val if val <= min | |
| val | |
| end | |
| def pop | |
| val = @main.pop | |
| @min.pop if min == val | |
| val | |
| end | |
| def min | |
| @min.last | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment