-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinStack.go
58 lines (51 loc) · 1.04 KB
/
MinStack.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
package main
type MinStack struct {
arr []int
minArr []int
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{
arr: []int{},
}
}
func (this *MinStack) Push(x int) {
this.arr = append(this.arr, x)
if len(this.minArr) == 0 {
this.minArr = append(this.minArr, x)
} else {
if this.minArr[len(this.minArr)-1] >= x {
this.minArr = append(this.minArr, x)
}
}
}
func (this *MinStack) Pop() {
if len(this.arr) == 0 {
return
}
p := this.arr[len(this.arr)-1]
this.arr = this.arr[:len(this.arr)-1]
if p == this.minArr[len(this.minArr)-1] {
this.minArr = this.minArr[:len(this.minArr)-1]
}
}
func (this *MinStack) Top() int {
if len(this.arr) == 0 {
return 0
}
return this.arr[len(this.arr)-1]
}
func (this *MinStack) Min() int {
if len(this.minArr) == 0 {
return 0
}
return this.minArr[len(this.minArr)-1]
}
/**
* Your MinStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.Min();
*/