Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.2 kB
3
Indexable
Never
int max(int a, int b){
        int max=a>b?a:b;
        return max;
    }
    
    int findLHS(vector<int>& nums) {
        unordered_map<int, vector<int>> um;
        
        
        for(int i = 0; i < nums.size(); i++){
           if(um.find(nums[i]) == um.end()){
               um[nums[i]].push_back(0);
           }
            if(um.find(nums[i]) != um.end()){
                 if((um.find(nums[i] - 1) != um.end())== true){
                   um[nums[i] - 1].push_back(nums[i]);
               }
               if((um.find(nums[i]) != um.end()) == true){
                  //some problem
                   
                   um[nums[i]].push_back(nums[i]);
               }
               if((um.find(nums[i]+1) != um.end()) == true){
                   um[nums[i]].push_back(nums[i]);
                 }
            } 
        }
        int m = 0;
        for(auto x: um){
            for(int i = 1; i < x.second.size(); i++){
                cout<<x.second[i];
            }
            cout<<" ";
            m = max(m, (x.second.size()-1));
            cout<<x.first<<" "<<m<<" "<<endl;
        }
       return (m);