Two stacks make it simply using ruby (96ms)


  • 0
    T
    class MinStack
        def initialize
            @mins, @stack = [], []
        end
    
        def push(x)
            @stack.push x
            @mins.push x if @mins.empty? or x <= @mins.last
        end
    
        def pop
            @mins.pop if @stack.pop == @mins.last
        end
    
        def top
            @stack.last
        end
    
        def get_min
            @mins.last
        end
    end
    

    @mins is the array used to save appeared minimums still existing in @stack.


  • 0
    M

    thanks beans, just to clarify, does pop still pop from stack if stack.pop doesnt equal mins.last?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.