Created
August 5, 2012 00:27
-
-
Save swuecho/3260814 to your computer and use it in GitHub Desktop.
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 Minstack { | |
| has @.stack; | |
| has @.min; | |
| method mpush($item) { | |
| push @.stack, $item; | |
| if @.min==0 or @.min[*-1] > $item { | |
| push @.min, $item; | |
| } | |
| } | |
| method mpop() { | |
| my $item=pop @.stack; | |
| if $item == @.min[*-1] { | |
| pop @.min; | |
| } | |
| return $item; | |
| } | |
| method mmin() { | |
| @.min[*-1]; | |
| } | |
| } | |
| my $min=Minstack.new; | |
| $min.mpush(5); | |
| $min.mpush(3); | |
| $min.mpush(10); | |
| say $min.mpop(); | |
| say $min.mmin(); | |
| =begin comment | |
| =end comment | |
| =begin comment | |
| Why this version does not work? | |
| my @min=Minstack.new; | |
| push @min, 5; | |
| push @min, 3; | |
| push @min, 10; | |
| push @min, 2; | |
| say(pop @min); # 2 | |
| say(min @min); # 3 | |
| say(pop @min); # 10 | |
| say(min @min); # 3 | |
| say(pop @min); # 3 | |
| say(min @min); # 5 | |
| say(pop @min); # 5 | |
| =end comment | |
| =begin comment | |
| #if @.stack==0 { | |
| # say "min stack is empty"; | |
| # return | |
| # } | |
| =end comment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment