Runtime Error, quite simple code


  • 0
    M
    class Solution {
    public:
        int maximumGap(vector<int> &num) {
            if (num.size()<2) return 0;
    
            int maxEle = *(std::max_element(num.begin(),num.end())),minEle = *(std::min_element(num.begin(),num.end())), size=(int)num.size(), bktSize = (maxEle-minEle)/(size-1);
            
            vector<int> nullVec;
            vector<vector<int>> bucket(size,nullVec);
            for (int i=0;i<size;i++){
                bucket[(num[i]-minEle)/bktSize].push_back(num[i]);
            }
            
            int maxGap = 0;
            for (int i=0,j=1;j<size;){
                if (bucket[j].size()==0) j++;
                else{
                    int small = *(std::max_element(bucket[i].begin(),bucket[i].end()));
                    int large = *(std::min_element(bucket[j].begin(),bucket[j].end()));
                    if ((large-small)>maxGap) maxGap = large-small;
                    i = j++;
                }
            }
            
            return maxGap;
        }
    };
    

    It gets runtime error with test case {3,6,1,9}. Any idea?
    ps. I can get correct answer without warning in IDE by this code.


Log in to reply
 

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