1-liner Python beat 98% (and 1-liner C++) using set


  • 0

    Make sure to give the sister one candy of each kind if not exceeding half of the total count:

        def distributeCandies(self, candies):
            return min(len(candies)/2, len(set(candies)))
    

    C++:

        int distributeCandies(vector<int>& candies) {
            return min(set<int>(candies.begin(), candies.end()).size(), candies.size()/2);
        }
    

Log in to reply
 

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