Related Topics

Data Structure
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val):
self.stack.append(val)
if not self.min_stack or val <= self.min_stack[-1]:
self.min_stack.append(val)
def pop(self):
if self.stack[-1] == self.min_stack[-1]:
self.min_stack.pop()
return self.stack.pop()
def top(self):
return self.stack[-1]
def get_min(self):
return self.min_stack[-1]
This implementation of the min-stack allows you to get the minimum element of the stack in constant time by using the get_min()
method.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.