Optimized C++ solution. Beats 95.4% of submissions


  • 0
    C
        int findLHS(vector<int>& nums) {
            
            unordered_map<int, int> counter;
           
            for(int it: nums)
            {
                counter[it]++;
            }
           
            int maxCount = 0;
            for(auto it1 : counter)
            {
                auto it2 = counter.find(it1.first + 1);
                if(it2 != counter.end())
                {
                    maxCount = max(maxCount, it1.second + it2->second);
                }
            }
            
            return maxCount;
        }
    

Log in to reply
 

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