What's wrong with my code?


  • 0
    J
    int findLHS(vector<int>& nums) {
    unordered_map<int,int> map;
    for(auto i:nums)
    map[i]++;
    int cnt=0;
    for(auto it:map)
    cnt = max(cnt,max(map[it.first-1]?(map[it.first-1]+map[it.first]):0,map[it.first+1]?(map[it.first]+map[it.first+1]):0));
    return cnt;
    }
    

    My code failed on the following testcase. But I got the correct answer when I run this code on my laptop.
    [1,2,1,3,0,0,2,2,1,3,3]. correct answer: 6. I always got 5 running on the webpage.
    What mistake did I make? Could anyone help me find out?


  • 0
    H

    can you make you're code more readable?


Log in to reply
 

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