[Run Time Error] on big test case C++

  • 0
    class Solution {
        int maximumGap(vector<int> &num) {
            if (num.size()<=2) return 0;
            unordered_set<int> pool;
            for (int i=0;i<num.size();pool.emplace(num[i++]));
            if (pool.size()==1) return 0;
            int maxNum = INT_MIN;
            int minNum = INT_MAX;
            for (auto it=pool.begin();it!=pool.end();it++){
                if ((*it)>maxNum) maxNum = *it;
                if ((*it)<minNum) minNum = *it;
            const int bucket_size = (maxNum-minNum)/(pool.size()-1);
            const int bucket_num = 1+((maxNum-minNum)/bucket_size);
            vector<int> bucket_max(bucket_num,INT_MIN);
            vector<int> bucket_min(bucket_num,INT_MAX);
            for (auto it = pool.begin();it!=pool.end();it++){
                int loc = (*it-minNum)/(pool.size()-1);
                if ((*it)>bucket_max[loc]) bucket_max[loc] = *it;
                if ((*it)<bucket_min[loc]) bucket_min[loc] = *it;
            int maxDiff = 0;
            for (int i=0;i<bucket_num-1;){
                int j = i+1;
                for (;j<bucket_num&&bucket_min[j]==INT_MAX;j++);
                if (j<bucket_num) maxDiff = max(maxDiff, bucket_min[j]-bucket_max[i]);
                i = j;
            return maxDiff;

    Any idea?
    Run time error on:

  • 0

    First, change this line:

    int loc = (*it-minNum)/(pool.size()-1);


    int loc = (*it-minNum)/bucket_size;

    Last, if you do not rely on INT_MAX and INT_MIN, your logic will be more clear. I don't want to fix your messy code.

  • 1

    I have get the same error during the submit, while it works well in the local.

    the most reason I find it's INT_MAX/INT_MIN, for the test data have INT_MAX in it, even I change it to LLONG_MAX, the same error I get.

    at last, I use -1 for the uninitialized slot, for the data must be "non-negative", and pass the test.

    but the reason while INT_MAX/LLONG_MAX will get Runtime Error in the online judge, I have no idea

Log in to reply

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