Problem Solving

85. Min Stack

굥깡 2023. 1. 17. 23:51
728x90

https://leetcode.com/problems/min-stack/

 

Min Stack - LeetCode

Min Stack - Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: * MinStack() initializes the stack object. * void push(int val) pushes the element val onto the stack. * void pop()

leetcode.com

71. Implement Queue using Stacks와 비슷한 문제지만 queue가 아닌 stack을 구현하는 문제

class MinStack:

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

    def push(self, val: int) -> None:
        self.stack.append(val)

    def pop(self) -> None:
        return self.stack.pop()

    def top(self) -> int:
        return self.stack[-1]

    def getMin(self) -> int:
        return min(self.stack)


# 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()

71. Implement Queue using Stacks와 구현 방식도 거의 비슷해서 쉽게 풀 수 있었음