Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Here is the code to solve this problem.
package main
type MinStack struct {
stack []int
min []int
}
func NewMinStack() *MinStack {
return &MinStack{stack: []int{}, min: []int{}}
}
// Push element x onto stack.
func (s *MinStack) Push(x int) {
s.stack = append(s.stack…