6ms c++ using priority queue


  • 0
    S
    public:
        int maximumGap(vector<int>& nums) {
            if(nums.size()<2) return 0;
            priority_queue<int> q;
            int maxVal= INT_MIN;
            int i=0;
            for(;i<nums.size()-1;i++){
                q.push(nums[i]);
            }
            int prev=nums[i];
            for(int i=0;i<nums.size();i++){
                if(prev-q.top()>maxVal){
                    maxVal=prev-q.top();
                }
                prev=q.top();
                q.pop();
            }
            return maxVal;
        }
    };

Log in to reply
 

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