Phew!!, finally avoided Cpp TLE, 1684ms solution


  • 0
    U
    class PhoneDirectory {
    public:
        /** Initialize your data structure here
            @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
            bool *status;
            int *elem;
            int index;
            int sz;
        PhoneDirectory(int maxNumbers) {
            status = new bool[maxNumbers];
            elem = new int[maxNumbers];
            index = 0;
            sz = maxNumbers;
            for(int i=0; i<sz; i++)
            {
                status[i] = true;
                elem[i] = i;
            }
        }
        
        /** Provide a number which is not assigned to anyone.
            @return - Return an available number. Return -1 if none is available. */
        int get() {
            if(index >= sz)
            {
                return -1;
            }
            int ret = elem[index];
            elem[index] = -1;
            status[ret] = false;
            index++;
            return ret;
        }
        
        /** Check if a number is available or not. */
        bool check(int number) {
            return status[number];
        }
        
        /** Recycle or release a number. */
        void release(int number) {
            if(!status[number])
            {
                status[number] = true;
                index--;
                elem[index] = number;
            }
        }
    };
    
    /**
     * Your PhoneDirectory object will be instantiated and called as such:
     * PhoneDirectory obj = new PhoneDirectory(maxNumbers);
     * int param_1 = obj.get();
     * bool param_2 = obj.check(number);
     * obj.release(number);
     */
    

Log in to reply
 

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