hugo-blog/content/posts/2020-03-29-leetcode-914.md
Ching 5ba7024532 feat(content; layouts; static): migrate hexo blog. add new theme fuji.
migrate hexo blog. add new theme fuji.

Signed-off-by: Ching <loooching@gmail.com>
2022-02-07 23:38:40 +08:00

922 B
Raw Blame History

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

914. 卡牌分组

题目

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

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