Skip to content

Commit

Permalink
solved: LeetCode #155 Min Stack on neetcode -@iamserda
Browse files Browse the repository at this point in the history
  • Loading branch information
iamserda committed Jan 26, 2025
1 parent d142bc3 commit 8ed5a76
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions neetcodeio/algostructybeginners/Lv1-Arrays/min_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ class MinStack:

def __init__(self):
self.stack = []
self.min_stack = []
self.minstack = []

def push(self, val: int) -> None:
self.stack.append(val)
if self.min_stack and val < self.min_stack[-1]:
popped = self.min_stack.pop()
self.min_stack.append(val)
self.min_stack.append(popped)
else:
self.min_stack.append(val)

if val is not None:
self.stack.append(val)
if not self.minstack:
self.minstack.append(val)
elif val < self.minstack[-1]:
self.minstack.append(val)
else:
self.minstack.append(self.minstack[-1])

def pop(self) -> None:
if self.stack:
popped = self.stack.pop()
self.min_stack.pop() if popped == self.min_stack[-1] else None
self.stack.pop()
self.minstack.pop()

def top(self) -> int:

if self.stack:
return self.stack[-1]

def getMin(self) -> int:

if self.stack:
return self.minstack[-1]


# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
# param_4 = obj.getMin()

0 comments on commit 8ed5a76

Please sign in to comment.