python solution simple enough and beats 98%


  • 0
    P

    '''

    def __init__(self):
        self.dd = collections.defaultdict(int)
    
    def inc(self, key):
        self.dd[key] += 1
    
    def dec(self, key):
        if self.dd[key] > 1:
            self.dd[key] -= 1
        else:
            self.dd.pop(key)
    
    def getMaxKey(self):
        try:
            maxval = max(self.dd.values())
            for i in self.dd:
                if self.dd[i] == maxval:
                    return i
        except:
            return ""
    
    def getMinKey(self):
        try:
            minval = min(self.dd.values())
            for i in self.dd:
                if self.dd[i] == minval:
                    return i
        except:
            return ""
    

    '''


  • 0
    S

    @pyufftj getMaxKey and getMinKey take O(n) time


Log in to reply
 

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