Created
February 7, 2013 14:14
-
-
Save tomgullo/4731152 to your computer and use it in GitHub Desktop.
stack that is fast for push, pop and min
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
l = [] | |
l2 = [] | |
def push(def num) { | |
if (l) { | |
l2 << ( (l[l.size() - 1] < num) ? l[l.size() -1] : num ) | |
} else { | |
l2 << num | |
} | |
l << num | |
} | |
def pop() { | |
def ret = l[l.size() - 1] | |
l.remove(l.size() -1) | |
l2.remove(l2.size() - 1) | |
return ret | |
} | |
def min() { | |
l2[l.size() -1] | |
} | |
push(6); push(3); push(7) | |
printit() | |
println "min " + min() | |
def printit() { | |
println l | |
println l2 | |
} | |
println "pop " + pop() | |
println min() | |
println "pop " + pop() | |
println min() | |
println l2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment