[Leetcode] Min Stack Problem in Golang

Jerry An
2 min readJan 28
Photo by James Harrison on Unsplash

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…
Jerry An

Golang/Python developer. To support me join Medium: https://jerryan.medium.com/membership or Buy me a coffee: https://ko-fi.com/jerryan