class MinStack:
def __init__(self):
self.min_value = []
self.stack = []
def push(self, val: int) -> None:
self.stack.append(val)
if len(self.min_value) == 0:
self.min_value.append(val)
else:
self.min_value.append(min(self.min_value[-1], val))
def pop(self) -> None:
self.stack.pop()
self.min_value.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_value[-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()
https://youtu.be/qkLl7nAwDPo?si=NAkNePjmgVyQa2ny
최종 코드
class MinStack:
def __init__(self):
self.val = []
self.minimum = []
def push(self, val: int) -> None:
if not self.minimum:
self.minimum.append(val)
else:
if val < self.minimum[-1]:
self.minimum.append(val)
else:
self.minimum.append(self.minimum[-1])
self.val.append(val)
def pop(self) -> None:
self.val.pop()
self.minimum.pop()
def top(self) -> int:
return self.val[-1]
def getMin(self) -> int:
return self.minimum[-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()'LeetCode > NeetCode' 카테고리의 다른 글
| Hashmap: 146. LRU Cache ★★★ (0) | 2024.12.10 |
|---|---|
| 21. Merge Two Sorted Lists (1) | 2024.12.07 |
| 20. Valid Parentheses (0) | 2024.12.02 |
| [Sliding Window Fixed Size] 219. Contains Duplicate II (1) | 2024.12.01 |
| Hashmap: 1. Two Sum ★ (1) | 2024.12.01 |