The same data structure and algorithm, C++ TLE, Python AC


  • 0

    Hmm, interesting....

    class PhoneDirectory(object):
        def __init__(self, maxNumbers):
            self._mark = [0]*maxNumbers
            self._q = []
            self._cur = -1
    
        def get(self):
            if len(self._q):
                val = heapq.heappop(self._q)
                self._mark[val] = 1
                return val
            else:
                if self._cur >= len(self._mark)-1: return -1
                self._cur += 1
                self._mark[self._cur] = 1
                return self._cur
    
        def check(self, number):
            if number >= len(self._mark): return False
            return not self._mark[number]
            
    
        def release(self, number):
            if number >= len(self._mark): return
            if self._mark[number]:
                self._mark[number] = False
                if number == self._cur: self._cur -= 1
                else: heapq.heappush(self._q, number)
    

Log in to reply
 

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