-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (58 loc) · 1.37 KB
/
main.go
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
package main
import (
"fmt"
)
type node struct {
val int
min int // every node is going to have current min
}
type MinStack struct {
stack []*node // in the stack we store not just value but additional info
}
func Constructor() MinStack {
return MinStack{}
}
// Push pushes the element val onto the stack.
func (s *MinStack) Push(val int) {
currentMin := val
// if there is something inside, then we use this as current min
if s.top() != nil {
currentMin = s.top().min
}
n := &node{
val: val,
min: min(val, currentMin), // with a new node we store current min
}
s.stack = append(s.stack, n)
}
// Pop removes the element on the top of the stack.
func (s *MinStack) Pop() {
s.stack = s.stack[:len(s.stack)-1] // [:), means last not included
}
// Top gets the top element of the stack.
func (s *MinStack) Top() int {
if s.top() == nil {
panic("should not happen based on constrains")
}
return s.top().val
}
func (s *MinStack) top() *node {
if len(s.stack) == 0 {
return nil
}
return s.stack[len(s.stack)-1]
}
// GetMin retrieves the minimum element in the stack.
func (s *MinStack) GetMin() int {
return s.top().min
}
func main() {
minStack := Constructor()
minStack.Push(-2)
minStack.Push(0)
minStack.Push(-3)
fmt.Println(minStack.GetMin()) // return -3
minStack.Pop()
fmt.Println(minStack.Top()) // return 0
fmt.Println(minStack.GetMin()) // return -2
}