One pass c++ solution


  • 0
    M
        int findLHS(vector<int>& nums) {
            unordered_map<int, int> hash;
            int ans = 0;
            for(int n : nums){
                hash[n]++;
                if(hash[n-1] != 0) ans = max(ans, hash[n] + hash[n-1]);
                if(hash[n+1] != 0) ans = max(ans, hash[n] + hash[n+1]);
            }
            return ans;
        }
    

Log in to reply
 

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