Why my python code is a wrong answer?


  • 0
    G
    class MinStack:
        # @param x, an integer
        # @return an integer
        arr = []
        minVal = []
        def push(self, x):
            if not self.minVal or x <= self.minVal[-1]:
                self.minVal.append(x)
            self.arr.append(x)
    
        # @return nothing
        def pop(self):
            if len(self.arr) > 0 and \
                self.arr.pop() <= self.minVal[-1]:
                self.minVal.pop()
    
        # @return an integer
        def top(self):
            if not self.arr:
                return None
            return self.arr[-1]
    
        # @return an integer
        def getMin(self):
            return self.minVal[-1]
    

    Submission Result: Wrong Answer

    Input:	push(-1),top,getMin
    Output:    [-1,-3]
    Expected:  [-1,-1]
    

    I try to run the same testcase locally:

    >>> m = MinStack()
    >>> m.push(-1)
    >>> m.top()
    -1
    >>> m.getMin()
    -1
    

    So,where is the '-3' come from?


  • 0

Log in to reply
 

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