Skip to content

Instantly share code, notes, and snippets.

@abdulrahmanAlotaibi
Created December 24, 2022 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulrahmanAlotaibi/b51e561e33b9ce4eccb6763fc67f2604 to your computer and use it in GitHub Desktop.
Save abdulrahmanAlotaibi/b51e561e33b9ce4eccb6763fc67f2604 to your computer and use it in GitHub Desktop.
Min Stack
type MinStack struct {
stack [][]int
top int
}
func Constructor() MinStack {
stack := [][]int{}
return MinStack{
stack:stack,
top:0,
}
}
func (this *MinStack) Push(val int) {
if len(this.stack) == 0 {
this.stack = append(this.stack, []int{val, val})
} else if val <= this.GetMin() {
this.stack = append(this.stack, []int{val, val})
}else {
this.stack = append(this.stack, []int{val, this.GetMin()})
}
this.top++
}
func (this *MinStack) Pop() {
this.stack = this.stack[:len(this.stack) - 1]
this.top--
}
func (this *MinStack) Top() int {
return this.stack[this.top-1][0]
}
func (this *MinStack) GetMin() int {
return this.stack[this.top-1][1]
}
/**
* 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();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment