Skip to content

Commit be78ba0

Browse files
committed
155
1 parent 3dc9984 commit be78ba0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

ruby/155-Min-Stack.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class MinStack
2+
def initialize
3+
@stack = []
4+
end
5+
6+
def push(val)
7+
min = if @stack.empty?
8+
val
9+
else
10+
val < @stack.last[1] ? val : @stack.last[1]
11+
end
12+
13+
@stack << [val, min]
14+
nil
15+
end
16+
17+
def pop
18+
@stack.pop
19+
nil
20+
end
21+
22+
def top
23+
return nil if @stack.empty?
24+
25+
@stack.last[0]
26+
end
27+
28+
def get_min
29+
return nil if @stack.empty?
30+
31+
@stack.last[1]
32+
end
33+
end

0 commit comments

Comments
 (0)