4-lines simple O(n) solution


  • 0
    M
        int distributeCandies(vector<int>& candies) {
            unordered_set<int> kinds;
            for(int i=0;i<candies.size();i++) kinds.insert(candies[i]);
            if(kinds.size()>=candies.size()/2) return candies.size()/2;
            else return kinds.size();
        }

Log in to reply
 

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