914. The card packet C ++ 5 lines to improve official solution to a problem of extreme simplicity

914. The card packet

Given a deck of cards, each card are written on an integer.

At this point, you need to select a digital X, so that we can be full decks of cards according to the following rules into one or more groups:

Each group has X cards.
Within the group are written the same integer on all the cards.
> = 2 returns true only if you optional X.

Example 1:

Input: [1,2,3,4,4,3,2,1]
Output: true
interpretation: grouping is possible [1,1], [2,2], [3,3], [4,4 ]

Example 2:

Input: [1,1,1,2,2,2,3,3]
Output: false
interpretation: the packet does not satisfy the requirements.

Example 3:

Input: [1]
Output: false
interpretation: the packet does not satisfy the requirements.

Example 4:

Input: [1,1]
Output: true
interpretation: grouping is possible [1,1]

Example 5:

Input: [1,1,2,2,2,2]
Output: true
interpretation: grouping is possible [1,1], [2,2], [2,2]

prompt:

1 <= deck.length <= 10000
0 <= deck[i] < 10000

Thinking

0:00 last night, too, I want questions to sleep. According to figures began to write most of the count, the number of all the same was true. Submit error, see [1,1,2,2,2,2] to understand, thoughtful consideration or not. He thought for a moment, as long as all of the number of cards exist> = number of 2 to the Convention. After submitting the last time the memory is 10%, very embarrassing. Here are just for the record, it is recommended to see the official explanations.
This article was written half the time I tested the code official, also after 10%, I take back the words above, ha ha ha.
The official solution to a problem is more than I clean a lot, but I think there is still room for improvement in the official explanations.
0 to any number GCD is itself, the use of this feature can skip all the cards in an amount of 0 omitted cnt [i] is determined non-zero, while if (~ g) is also omitted in the circulation, the code extremely simple, very clever method.
Any number and gcd 0 is itself, these words may make you feel confused, do not worry, here to talk about the need to implement the gcd.
I do not know C ++ comes with gcd gcd when my own so long, after reading you will understand.

inline int gcd(int a, int b) {
    if (b) while ((a %= b) && (b %= a));
    return a + b;
}

C ++ built __gcd () is the estimated phase Nianzhuan division.
And this solution to a problem of official comment so I laughed aloud.
Here Insert Picture Description

Improved official explanations

class Solution {
    int cnt[10000];
public:
    bool hasGroupsSizeX(vector<int>& deck) {
        for (auto x: deck) cnt[x]++;
        int g = 0;
        for (int i = 0; i < 10000; ++i)
            g = gcd(g, cnt[i]);
        return g >= 2;
    }
};

My ( after 10% ) answer

class Solution {
public:
    bool hasGroupsSizeX(vector<int>& deck) {
        map<int, int> cards;
        for (auto num:deck){
            if (cards.count(num) != 0)
                cards[num]++;
            else
                cards[num] = 1;
        }
        int cnt = cards.begin()->second;
        for (auto card : cards)
            if (card.second != cnt)
                return false;
        return true;
    }
};
Published 38 original articles · won praise 0 · Views 1016

Guess you like

Origin blog.csdn.net/Cyan1956/article/details/105134780
Recommended