---
title: leetcode-914
date: 2020-03-29 22:41:09
tags:
categories: leetcode
---

### 914. 卡牌分组

[题目](https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/)



<!--more-->



将大牌堆分成多个牌数量相等的小牌堆，就是求每张牌数量的公约数。先遍历一遍得到每张牌的数量，然后找出比2大的公约数即可。



```python
class Solution:
  def hasGroupsSizeX(self, deck) -> bool:
    dc = {}
    max_d = 0
    for d in deck:
      if d not in dc:
        dc[d] = 0
      dc[d] += 1
      if max_d < d:
        max_d = d
      if max_d < dc[d]:
        max_d = dc[d]
    has_x = True
    if max_d == 1:
      max_d = 2

    for i in range(2, max_d + 1):
      has_x = True
      for k,v in dc.items():
        if v % i:
          has_x = False
          break
      if has_x and i >= 2:
        return True
    return False

#56 ms	13.8 MB
```

