Table of contents
Problem - Leetcode
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack
class:
MinStack()
initializes the stack object.void push(int val)
pushes the elementval
onto the stack.void pop()
removes the element on the top of the stack.int top()
gets the top element of the stack.int getMin()
retrieves the minimum element in the stack.
You must implement a solution with O(1)
time complexity for each function.
Example 1:
Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output
[null,null,null,null,-3,null,0,-2]
Explanation
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
Constraints:
-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1
Methods
pop
,top
andgetMin
operations will always be called on non-empty stacks.At most
3 * 10<sup>4</sup>
Calls will be made topush
,pop
,top
, andgetMin
.
Solution in Golang
type MinStack struct {
stack []int
minStack []int
}
func Constructor() MinStack {
return MinStack{}
}
func (this *MinStack) Push(val int) {
this.stack = append(this.stack, val)
if len(this.minStack) == 0 || val <= this.minStack[len(this.minStack)-1] {
this.minStack = append(this.minStack, val)
}
}
func (this *MinStack) Pop() {
if this.isEmpty() {
return
}
popped := this.stack[len(this.stack)-1]
this.stack = this.stack[:len(this.stack)-1]
if popped == this.minStack[len(this.minStack)-1] {
this.minStack = this.minStack[:len(this.minStack)-1]
}
}
func (this *MinStack) Top() int {
if this.isEmpty() {
return 0
}
return this.stack[len(this.stack)-1]
}
func (this *MinStack) GetMin() int {
if this.isEmpty() {
return 0
}
return this.minStack[len(this.minStack)-1]
}
func (this *MinStack) Len() int {
return len(this.stack)
}
func (this *MinStack) isEmpty() bool {
return this.Len() == 0
}
This code defines a data structure called MinStack
is designed to support efficient retrieval of the minimum element in a stack of integers. It uses two stacks, stack
and minStack
, to achieve this functionality.
Here's a detailed explanation of each part of the code:
type MinStack struct
: This line defines a struct namedMinStack
, which contains two fields:stack
: This field is a slice (dynamic array) that will store the elements of the main stack.minStack
: This field is another slice that will store the minimum elements encountered so far in the main stack.
func Constructor() MinStack
: This is a constructor function that initializes and returns an instance of theMinStack
struct. It simply returns an emptyMinStack
by callingMinStack{}
.func (this *MinStack) Push(val int)
: This method is used to push an integer valueval
onto the main stack. It does the following:Appends
val
to thestack
.Checks if
minStack
is empty or ifval
is less than or equal to the current minimum value stored inminStack
. If true, it also appendsval
tominStack
.
func (this *MinStack) Pop()
: This method is used to pop the top element from the main stack. It does the following:Check if the main stack is empty (using the
isEmpty()
helper method) and returns if it is.Pops the top element from the
stack
.Check if the popped element is equal to the current minimum value stored in
minStack
. If true, it also pops the top element fromminStack
.
func (this *MinStack) Top() int
: This method returns the top element of the main stack without removing it. It checks if the stack is empty and returns 0 if it is.func (this *MinStack) GetMin() int
: This method returns the current minimum element in the main stack (the top element ofminStack
). It also checks if the stack is empty and returns 0 if it is.func (this *MinStack) Len() int
: This method returns the length (number of elements) of the main stack.func (this *MinStack) isEmpty() bool
: This is a helper method that checks if the main stack is empty by comparing its length to 0. It returnstrue
if the stack is empty,false
otherwise.
In summary, this code defines a specialized stack data structure that maintains two stacks: one for the main stack and another for keeping track of the minimum element. This allows for efficient retrieval of the minimum element in constant time while supporting standard stack operations like push and pop.