Advertisement
lazar955

Untitled

Oct 24th, 2023
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.99 KB | None | 0 0
  1. type MinStack struct {
  2.      stack []int
  3.      mins []int
  4. }
  5.  
  6.  
  7. func Constructor() MinStack {
  8.     return MinStack{
  9.         stack: []int{},
  10.         mins: []int{},
  11.     }
  12. }
  13.  
  14.  
  15. func (this *MinStack) Push(val int)  {
  16.     this.stack = append(this.stack, val)
  17.     if len(this.mins) > 0 {
  18.         this.mins = append(this.mins, min(this.mins[len(this.mins)-1],val))
  19.     }else {
  20.         this.mins = append(this.mins,val)
  21.     }
  22. }
  23.  
  24.  
  25. func (this *MinStack) Pop()  {
  26.     this.stack = this.stack[:len(this.stack)-1]
  27.     this.mins = this.mins[:len(this.mins)-1]
  28. }
  29.  
  30.  
  31. func (this *MinStack) Top() int {
  32.     return this.stack[len(this.stack)-1]
  33. }
  34.  
  35.  
  36. func (this *MinStack) GetMin() int {
  37.     return this.mins[len(this.mins)-1]
  38. }
  39.  
  40. func min(l,r int) int {
  41.     if l > r {
  42.         return r
  43.     }
  44.     return l
  45. }
  46.  
  47.  
  48. /**
  49.  * Your MinStack object will be instantiated and called as such:
  50.  * obj := Constructor();
  51.  * obj.Push(val);
  52.  * obj.Pop();
  53.  * param_3 := obj.Top();
  54.  * param_4 := obj.GetMin();
  55.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement