Python


  • 0
    D
    def __init__(self):
            """
            initialize your data structure here.
            """
            self.stack = []
            self.minheap = []
            
    
        def push(self, x):
            """
            :type x: int
            :rtype: void
            """
            self.stack.append(x)
            heapq.heappush(self.minheap, x)
            
    
        def pop(self):
            """
            :rtype: void
            """
            if self.stack:
                val = self.stack.pop()
            if self.minheap:
                self.minheap.remove(val)
                heapq.heapify(self.minheap)
    
        def top(self):
            """
            :rtype: int
            """
            if self.stack:
                return self.stack[-1]
    
        def getMin(self):
            """
            :rtype: int
            """
            return self.minheap[0]
    

  • 0
    P

    This solution should not be accepted since adding / deleteing from a heap is not O(1).


Log in to reply
 

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