C++ Solution


  • 0
    Z
    class Solution {
    public:
        int distributeCandies(vector<int>& candies) {
            unordered_set<int> candies_set;
            int sis_candies_num = 0, max_num = candies.size() >> 1, i = 0;
            for(; i < max_num; ++i) {
                if(candies_set.find(candies[i]) == candies_set.end())   ++sis_candies_num;
                candies_set.insert(candies[i]);
            }
            for(; sis_candies_num < max_num && i < candies.size(); ++i) {
                if(candies_set.find(candies[i]) == candies_set.end())   ++sis_candies_num;
                candies_set.insert(candies[i]);
            }
            return sis_candies_num;
        }
    };
    

Log in to reply
 

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