type MinStack struct {
stack []int
minIndex int
length int
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{}
}
func (this *MinStack) Push(val int) {
this.stack = append(this.stack, val)
this.minIndex = this.calculateMinIndex()
this.length++
}
func (this *MinStack) Pop() {
this.stack = this.stack[:len(this.stack)-1]
this.minIndex = this.calculateMinIndex()
this.length--
}
func (this *MinStack) Top() int {
return this.stack[this.length-1]
}
func (this *MinStack) GetMin() int {
return this.stack[this.minIndex]
}
func (this *MinStack)calculateMinIndex()int{
var min_index = 0
for i:=1;i<len(this.stack);i++{
if this.stack[i]<this.stack[min_index]{
min_index = i
}
}
return min_index
}
type MinStack struct {
min int
stack list.List
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{min:math.MaxInt64}
}
func (this *MinStack) Push(val int) {
if val<this.min{
this.min = val
}
this.stack.PushBack(val)
}
func (this *MinStack) Pop() {
if this.min==this.stack.Back().Value.(int){
this.stack.Remove(this.stack.Back())
this.min = math.MaxInt64
for e:=this.stack.Front();e!=nil;e=e.Next(){
val := e.Value.(int)
if val<this.min{
this.min = val
}
}
}else{
this.stack.Remove(this.stack.Back())
}
}
func (this *MinStack) Top() int {
return this.stack.Back().Value.(int)
}
func (this *MinStack) GetMin() int {
return this.min
}