Created
December 24, 2022 03:36
-
-
Save abdulrahmanAlotaibi/b51e561e33b9ce4eccb6763fc67f2604 to your computer and use it in GitHub Desktop.
Min Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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