We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3dc9984 commit be78ba0Copy full SHA for be78ba0
ruby/155-Min-Stack.rb
@@ -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
12
13
+ @stack << [val, min]
14
+ nil
15
16
17
+ def pop
18
+ @stack.pop
19
20
21
22
+ def top
23
+ return nil if @stack.empty?
24
25
+ @stack.last[0]
26
27
28
+ def get_min
29
30
31
+ @stack.last[1]
32
33
+end
0 commit comments