Wrong answer with python code??


  • 0
    B

    Hello everyone,

    I got wrong answer with this code but it works well in my own pc and the same idea in java works well in leetcode as well. Could someone point out the problem? Thanks.

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

     class MinStack:
            intStack  = []
            minList = []
            
            # @param x, an integer
            # @return an integer
            def push(self, x):
                self.intStack.append(x)
                
                if len(self.minList) == 0:
                    self.minList.append(x)
                else:
                    if x < self.minList[-1]:
                        self.minList.append(x)
                    else:
                        self.minList.append(self.minList[-1])
                return x
        
            # @return nothing
            def pop(self):
                if len(self.intStack)==0:
                    return
                self.intStack.pop()
                self.minList.pop()
        
            # @return an integer
            def top(self):
                return self.intStack[-1]
        
            # @return an integer
            def getMin(self):
                return self.minList[-1]

  • 0
    C

    for top and getMin, you should check the size of minList as well.


  • 0

Log in to reply
 

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