C++ easy to understand hash table solution


  • 0
    Z
        int distributeCandies(vector<int>& candies) {
            int res=0;
            int num=candies.size();
            unordered_map<int,int>m;
            for(int i=0;i<num;i++) m[candies[i]]++;
            for(auto x:m){
                if(x.second>0) res++;
                if(res==num/2) break;
            }
            return res;
        }
    

Log in to reply
 

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