[Leetcode]155. Min Stack
2024. 8. 1. 21:11ㆍAlgorithm
1. problem :
https://leetcode.com/problems/min-stack/
2. solution 1 :
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
if not self.min_stack or val <= self.min_stack[-1]:
self.min_stack.append(val)
def pop(self) -> None:
if self.stack:
val = self.stack.pop()
if val == self.min_stack[-1]:
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1] if self.stack else None
def getMin(self) -> int:
return self.min_stack[-1] if self.min_stack else None
'Algorithm' 카테고리의 다른 글
[Leetcode]22. Generate Parentheses(x) (0) | 2024.08.02 |
---|---|
[Leetcode]150. Evaluate Reverse Polish Notation (0) | 2024.08.01 |
[Leetcode]20. Valid Parentheses (0) | 2024.08.01 |
[Leetcode]42. Trapping Rain Water(x) (0) | 2024.08.01 |
[Leetcode]167. Two Sum II - Input Array Is Sorted (0) | 2024.07.31 |