-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.go
50 lines (41 loc) · 886 Bytes
/
solution.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
package min_stack
type withMin struct {
val int
min int
}
type MinStack struct {
stack []withMin
}
func Constructor() MinStack {
return MinStack{stack: []withMin{}}
}
func (m *MinStack) Push(val int) {
if len(m.stack) == 0 {
m.stack = append(m.stack, withMin{val: val, min: val})
return
}
min := m.stack[len(m.stack)-1].min
if val > min {
m.stack = append(m.stack, withMin{val: val, min: min})
} else {
m.stack = append(m.stack, withMin{val: val, min: val})
}
return
}
func (m *MinStack) Pop() {
m.stack = m.stack[:len(m.stack)-1]
}
func (m *MinStack) Top() int {
return m.stack[len(m.stack)-1].val
}
func (m *MinStack) GetMin() int {
return m.stack[len(m.stack)-1].min
}
/**
* Your MinStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(val);
* obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.GetMin();
*/