My heap based CPP solution - beats 96.29% of cpp submissions


  • 0
    D
    class Solution {
    public:
        int longestConsecutive(vector<int>& nums) 
        {   
          if (nums.size() == 0) return 0;
    
          make_heap(nums.begin(), nums.end()); 
    
          auto prev = nums.front();
    
          pop_heap(nums.begin(), nums.end()); nums.pop_back();
    
          int count = 1;  
    
          int maxCount = 1;
    
          while (nums.size()) {
    
            auto curr = nums.front();
    
            if (curr == (prev - 1)) ++count;
            else if (curr != prev) count = 1;
    
            maxCount = max(maxCount, count);
    
            prev = curr;
    
            pop_heap(nums.begin(), nums.end()); nums.pop_back();
    
          }   
    
          return maxCount;
        }   
    };
    

Log in to reply
 

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