当然C++可以利用stl容器中的set或者unordered_set进行筛选,并用min方法返回种类和n/2中较小的那个。 1classSolution {2public:3intdistributeCandies(vector<int>&candies) {4unordered_set<int>kinds;5for(intkind : candies) {6kinds.insert(kind);7}8returnmin(kinds.size(), candies.size() /2);9}10};...
class Solution { public: int distributeCandies(vector<int>& candies) { int num = candies.size() / 2; int sort = 0; map<int, int> maps; auto iter = candies.begin(); while (iter != candies.end()) { if (maps.find(*iter) == maps.end()) { sort++; maps.insert(pair<int, int...
1classSolution(object):2defdistributeCandies(self, candies):3"""4:type candies: List[int]5:rtype: int6"""7kinds =len( set(candies) )8nums = len(candies)/29ifkinds<nums:10returnkinds11else:12returnnums131415if__name__=='__main__':16s =Solution()17res = s.distributeCandies([1,1...
Distribute Candies问题? Leetcode 575. Distribute Candies的时间复杂度是多少? 文章作者:Tyan 博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution **解析:**Version 1,使用dict。 Version 1 代码语言:javascript 代码运行次数:0 运行 AI代码解释 class Solution: def distributeCandies(self, candy...
class Solution: def distributeCandies(self, candies): """ :type candies: List[int] :rtype: int """ # k=len(set(candies)) # if k<=len(candies)/2: # return k # else: # return int(len(candies)/2) return len(set(candies)) if len(set(candies))<=len(candies)/2 else int(len...
Can you solve this real interview question? Distribute Candies - Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor. The doctor advised Alice to only eat n / 2 of the
class Solution { public int distributeCandies(int[] candies) { Map<Integer, Integer> table = new HashMap<>(); for (int candy : candies) { table.put(candy, table.getOrDefault(candy, 0) + 1); } return table.size() >= candies.length / 2 ?
# Time: O(n + logc), c is the number of candies # Space: O(1) class Solution(object): def distributeCandies(self, candies, num_people): """ :type candies: int :type num_people: int :rtype: List[int] """ # find max integer p s.t. sum(1 + 2 + ... + p) <= C #...
575. Distribute Candies* 575. Distribute Candies* https://leetcode.com/problems/distribute-candies/ 题目描述 Given an integer array with even length, where different numbers in this array represent different kinds of candies. Eac...575. Distribute Candies 题目分析:有偶数个糖果,哥哥和妹妹平分。
classSolution{publicintdistributeCandies(int[]candies){HashSet<Integer>set=newHashSet<Integer>();for(inti=0;i<candies.length;i++){set.add(candies[i]);}if(set.size()>=candies.length/2)returncandies.length/2;returnset.size();}}