-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path153MinStack.go
50 lines (43 loc) · 1.04 KB
/
153MinStack.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
//https://leetcode.com/problems/min-stack/description/
type MinStack struct {
minVal int
stack []int
}
func Constructor() MinStack {
return MinStack{
stack: []int{},
minVal: math.MaxInt32,
}
}
func (this *MinStack) Push(val int) {
if val <= this.minVal {
this.stack = append(this.stack, this.minVal)
this.minVal = val
}
this.stack = append(this.stack, val)
}
func (this *MinStack) Pop() {
if this.stack[len(this.stack)-1] == this.minVal {
//remove top and next and assign next to min
this.stack = this.stack[:len(this.stack)-1]
//remove next and assign it to min
this.minVal = this.stack[len(this.stack)-1]
this.stack = this.stack[:len(this.stack)-1]
} else {
this.stack = this.stack[:len(this.stack)-1]
}
}
func (this *MinStack) Top() int {
return this.stack[len(this.stack)-1]
}
func (this *MinStack) GetMin() int {
return this.minVal
}
/**
* 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();
*/