-
Notifications
You must be signed in to change notification settings - Fork 1
/
min_stack.rb
80 lines (63 loc) · 1.39 KB
/
min_stack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'pry'
require 'pp'
class Stack
attr_reader :stack
def initialize
@stack = []
end
def pop
@stack.pop
end
def push(element)
@stack.push(element)
end
def peek
@stack[-1]
end
end
# sudo code:
# 1. initialize two stacks, one to track all elements, the other to track the most recent lowest element
# 2. when push element, compare the top element of min_stack with element
# if top element of min_stack is less than element, add top element to min_stack
# otherwise add element to the min_stack
# Also, add the element to the stack
# 3. When pop, pop the min_stack first, and pop stack and return its value
# 4. min function will be tracking the most recent element in min_stack
class MinStack
attr_reader :min_stack, :stack
def initialize
@min_stack = Stack.new
@stack = Stack.new
end
def push(element)
if @min_stack.peek && @min_stack.peek < element
@min_stack.push(@min_stack.peek)
else
@min_stack.push(element)
end
@stack.push(element)
end
def pop
@min_stack.pop
@stack.pop
end
def min
@min_stack.peek
puts "The minimun is #{@min_stack.peek}"
end
end
min_stack = MinStack.new
min_stack.push(2)
min_stack.push(1)
min_stack.push(7)
min_stack.push(3)
min_stack.push(4)
min_stack.min
min_stack.pop
min_stack.min
min_stack.pop
min_stack.min
min_stack.pop
min_stack.min
min_stack.pop
min_stack.min