From 4d29858e37351eedac39ba5cc024a5a0eb849d64 Mon Sep 17 00:00:00 2001 From: Soumen sinha mahapatra Date: Mon, 21 Sep 2020 12:37:05 +0530 Subject: [PATCH 1/2] Create minStack.py Famous Interview question to design a stack that can return minimum element in Stack in constant time --- data_structures/stacks/minStack.py | 71 ++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 data_structures/stacks/minStack.py diff --git a/data_structures/stacks/minStack.py b/data_structures/stacks/minStack.py new file mode 100644 index 000000000000..723ae42dd842 --- /dev/null +++ b/data_structures/stacks/minStack.py @@ -0,0 +1,71 @@ +''' + So MinStack is a Stack through which we can get minimum element in Constant time, we will maintain another stack to return Minimum + Element in O(1) time + +''' +class MinStack: + + + def __init__(self): + ''' Creating stack and min Stack, and size variable to keep track of no of elements in stack''' + self.stack = [] + self.minStack =[] + self.size =0 + + def push(self, data:int) -> None: + if self.size == 0: + self.minStack.append(data) + elif data <= self.minStack[-1]: + self.minStack.append(data) + self.stack.append(data) + self.size+=1 + + def pop(self)-> None: + ''' Removes the topmost element from the Stack ''' + top = self.stack.pop() + self.size-=1 + if top <= self.minStack[-1]: + self.minStack.pop() + + + def top(self) ->None: + ''' Returns the top element from the stack ''' + return self.stack[-1] + + def getMin(self): + ''' Returns the top element from the Minstack ''' + return self.minStack[-1] + +# Code execution starts here +if __name__ == "__main__": + + + # Creating a min Stack + stack = MinStack() + + stack.push(6) + print(stack.getMin()) # prints 6 + + stack.push(7) + print(stack.getMin()) # prints 6 + + stack.push(8) + print(stack.getMin()) # prints 6 + + stack.push(5) + print(stack.getMin()) # prints 5 + + stack.push(3) + print(stack.getMin()) # prints 3 + + stack.pop() + print(stack.getMin()) # prints 5 + + stack.push(10) + print(stack.getMin()) # prints 5 + + stack.pop() + print(stack.getMin()) # prints 5 + + stack.pop() + print(stack.getMin()) # prints 6 From 5d289ad8abb7c90034d7ec8c7ee6872ed19a2918 Mon Sep 17 00:00:00 2001 From: Soumen sinha mahapatra Date: Mon, 21 Sep 2020 13:07:54 +0530 Subject: [PATCH 2/2] Create minStack.py --- data_structures/stacks/minStack.py | 42 ++++++++++++------------------ 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/data_structures/stacks/minStack.py b/data_structures/stacks/minStack.py index 723ae42dd842..0cbbcc40ed04 100644 --- a/data_structures/stacks/minStack.py +++ b/data_structures/stacks/minStack.py @@ -1,45 +1,37 @@ -''' - So MinStack is a Stack through which we can get minimum element in Constant time, we will maintain another stack to return Minimum - Element in O(1) time - -''' class MinStack: - - def __init__(self): - ''' Creating stack and min Stack, and size variable to keep track of no of elements in stack''' + """ Creating stack minStack and size Variable""" self.stack = [] - self.minStack =[] - self.size =0 + self.minStack = [] + self.size = 0 - def push(self, data:int) -> None: - if self.size == 0: + def push(self, data: int) -> None: + if self.size == 0: self.minStack.append(data) elif data <= self.minStack[-1]: self.minStack.append(data) self.stack.append(data) - self.size+=1 - - def pop(self)-> None: - ''' Removes the topmost element from the Stack ''' - top = self.stack.pop() - self.size-=1 + self.size = self.size + 1 + + def pop(self) -> None: + """Removes the topmost element from the Stack """ + top = self.stack.pop() + self.size = self.size - 1 if top <= self.minStack[-1]: self.minStack.pop() - - - def top(self) ->None: - ''' Returns the top element from the stack ''' + + def top(self) -> None: + """Returns the top element from the stack """ return self.stack[-1] def getMin(self): - ''' Returns the top element from the Minstack ''' + """Returns the top element from the Minstack """ return self.minStack[-1] + # Code execution starts here if __name__ == "__main__": - - + # Creating a min Stack stack = MinStack()