simple c++ one pass solution


  • 0
    B
    int findLHS(vector<int> &nums) {
        unordered_map<int, int> _map;
    
        int maxCount(0);
        for (int num:nums) {
            _map[num]++;
            int localmax = max(_map[num - 1] ? _map[num - 1] + _map[num] : 0,
                               _map[num + 1] ? _map[num + 1] + _map[num] : 0);
            maxCount = max(maxCount, localmax);
        }
    
        return maxCount;
    }
    

Log in to reply
 

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