leetcode-575 - Distribute Candies (fast way to calculate the kind of elements in an array)

Topic description:

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.

Example 1:

Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
The sister has three different kinds of candies.

 

Example 2:

Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
The sister has two different kinds of candies, the brother has only one kind of candies.

 

Note:

  1. The length of the given array is in range [2, 10,000], and will be even.
  2. The number in given array is in range [-100,000, 100,000].

 

Function to be done:

int distributeCandies(vector<int>& candies) 

 

illustrate:

1. This question gives a vector, each number in it represents a kind of candy, such as [1,1,2,2,3,3], which means 1 candy has 2, 2 candy has 2, 3 candy has 2 2. The length of this vector must be an even number. The candy should be equally distributed to the elder brother and the younger sister, and the half of the candy that the younger sister can share can be as many as possible.

2. If we know that there are n kinds of candies, my sister can get m candies. If n<=m, it means that there are many repetitions, such as [1,1,1,1,2,2], my sister can get it There are 3 candies, and there are only 2 kinds of candies, so the maximum number of kinds that my sister can get will not exceed n, and I can only get some more duplicates.

If n>m, that is to say, there are more types of candies than the number of candies that my sister can get, it means that there are many kinds of candies, such as [1,2,3,4,5,5], my sister can get it There are 3 candies, and there are 5 kinds of candies, so the maximum number of kinds that my sister can get is only 3 kinds.

With a clear idea, we can construct the following code:

    int distributeCandies(vector<int>& candies) 
    {
        int total=candies.size()/2;
        set<int>s1(candies.begin(),candies.end());
        int kind=s1.size();
        if(kind<=total)
            return kind;
        else
            return total;
    }

This is the easiest way to implement, using set to get the number of categories.

The above code measured 333 ms, beats 30.13% of cpp submissions.

 

3. Improvement:

When we use set, we actually add the elements in the vector one by one. Every time we encounter an element, we judge whether the element has appeared before. If there is, we will not add it, and if not, we will add it. This process of judgment is actually a cycle.

So the method of set is actually a double loop, O(n^2).

 

But we don't actually need to deal with numbers, all we need is to know how many numbers are in the vector.

So we can actually do a quick sort on the vector, and then do a single loop. If the previous number is different from the next number, then the number of categories +1.

The method of sorting + single loop in this way has a time complexity lower than O(n^2).

 

code show as below:

    int distributeCandies(vector<int>& candies) 
    {
        int total=candies.size()/2;
        sort(candies.begin(),candies.end());
        int kind=1;
        for(int i=0;i<candies.size()-1;i++)
        {
            if(candies[i+1]!=candies[i])
                kind++;
        }
        if(kind<=total)
            return kind;
        else
            return total;
    }

上述代码实测258ms,beats 82.50% of cpp submissions。

 

4、另一种方法:

因为题目限定了数的范围在[-100,000,100,000],所以其实我们可以开辟一个长度为200001的vector。

接着迭代给定vector,更新长度为200001的vector的值。

最后再迭代这个长vector,看一下有多少种。

但是由于长vector长度太长了,所以这种方法花费时间很多,不是很推荐。这里只是做一个扩展介绍。

这道题的启示还是:当碰到需要判断vector中有多少种数字时,可以先做一个快速排序,接着单重循环。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325296483&siteId=291194637