Why cannot we use 2 hashsets?


  • -1
    S
    class PhoneDirectory {
    public:
        /** Initialize your data structure here
            @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
        unordered_set<int> used, notUsed;
        PhoneDirectory(int maxNumbers) {
            used.clear();
            for (int i = 0; i < maxNumbers; i++)
                notUsed.insert(i);
        }
        
        /** Provide a number which is not assigned to anyone.
            @return - Return an available number. Return -1 if none is available. */
        int get() {
            if (notUsed.empty())
                return -1;
            int ans = *notUsed.begin();
            used.insert(ans);
            notUsed.erase(ans);
            return ans;
        }
        
        /** Check if a number is available or not. */
        bool check(int number) {
            return used.find(number) == used.end();
        }
        
        /** Recycle or release a number. */
        void release(int number) {
            notUsed.insert(number);
            used.erase(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.