C++ Use sort and unique, no HashTable


  • 0
        int distributeCandies(vector<int>& candies)
        {
            sort(candies.begin(), candies.end());
            auto it = unique(candies.begin(), candies.end());
            int distance = it - candies.begin();
            return min(distance, (int)candies.size() / 2);
        }
    

    Sort first, then use the unique method to move the duplicates to the last so that you can get the count of the unique numbers.


Log in to reply
 

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