# 914. X of a Kind in a Deck of Cards

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/contest/weekly-contest-104/problems/x-of-a-kind-in-a-deck-of-cards/

> 内容描述

```
In a deck of cards, each card has an integer written on it.

Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:

Each group has exactly X cards.
All the cards in each group have the same integer.
 

Example 1:

Input: [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]
Example 2:

Input: [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.
Example 3:

Input: [1]
Output: false
Explanation: No possible partition.
Example 4:

Input: [1,1]
Output: true
Explanation: Possible partition [1,1]
Example 5:

Input: [1,1,2,2,2,2]
Output: true
Explanation: Possible partition [1,1],[2,2],[2,2]

Note:

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

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******



按照纸牌种类算一下每种纸牌的数量，然后有几下几个要点

1. 如果最少的纸牌数小于2，肯定返回False
2. 求出最小的纸牌数之后，如果任意一个纸牌数与它的最大公约数小于2，也直接返回False


因为数字都小于10000，所以求最大公约数的时间复杂度可以看成O(1)，因此最后的时间复杂度为O(N)

```python
class Solution(object):
    def hasGroupsSizeX(self, deck):
        """
        :type deck: List[int]
        :rtype: bool
        """
        lookup = collections.Counter(deck)
        min_num = min(lookup.values())
        if min_num < 2:
            return False
        def gcd(a, b):
            if a % b == 0:
                return b
            return gcd(b, a%b)
        for value in lookup.values():
            if gcd(value, min_num) < 2:
                return False
        return True
```
