-
Notifications
You must be signed in to change notification settings - Fork 1
/
minstack.go
50 lines (40 loc) · 1.03 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
package s0155_min_stack
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) #2024_03_22_Time_12_ms_(79.58%)_Space_7.4_MB_(21.27%)
import "math"
type element struct {
Val int
Min int
}
type MinStack struct {
Stack []element
Min int
}
func Constructor() MinStack {
return MinStack{[]element{}, math.MaxInt32}
}
func (this *MinStack) Push(val int) {
this.Stack = append(this.Stack, element{val, this.Min})
this.Min = min(this.Min, val)
}
func (this *MinStack) Pop() {
l := len(this.Stack)
this.Min = this.Stack[l-1].Min
this.Stack = this.Stack[:l-1]
}
func (this *MinStack) Top() int {
l := len(this.Stack)
return this.Stack[l-1].Val
}
func (this *MinStack) GetMin() int {
return this.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();
*/