Can anyone show me why I am getting TLE ?


  • 0
    J

    ......

    class PhoneDirectory {
    private:
        unordered_set <int> mset;
        int maxn;
        unordered_set<int>::iterator it;
    public:
        PhoneDirectory(int maxNumbers) {
            maxn = maxNumbers;
            mset.clear();
            for(int i = 0; i < maxn; i++)
            {
                mset.insert(i);
            }
        }
        int get() {
            it = mset.begin();
    
            
            if(it == mset.end())
            {
                return -1;
            }
            
            
            int re = *it;
            unordered_set<int>::iterator tmp = it;
            it++;
            mset.erase(tmp);
            return re;
        }
        
        bool check(int number) {
            if(mset.find(number)!= mset.end())
            return true;
            else
            return false;
            
        }
        void release(int number) {
            mset.insert(number);
        }
    };
    .......
    

    Can anyone show me why I am getting TLE ?
    TY


  • 0
    T

    @jersey

    If there is no error in you code, it is probably not your problem.

    I guess, OJ is not as optimized for C++ as for JAVA, that's why for the same algorithm, sometimes c++ implementation is much slower than java implementation.

    Please try to implement your algorithm using array instead of unordered_set, the performance would be better. (Though their time complexity is the same).

    You can find similar discussion here and here.


Log in to reply
 

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