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

2021-09-06 15:39:31 浏览数 (1)

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书

1. Description

2. Solution

**解析:**Version 1,统计元素个数,遍历所有可能的分割数量,下限为2,上限为最少的元素个数,如果满足条件,返回True

  • Version 1
代码语言:javascript复制
class Solution:
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        stat = collections.Counter(deck)
        for j in range(2, min(stat.values())   1):
            flag = True
            for val in stat.values():
                if val % j != 0:
                    flag = False
                    break
            if flag:
                return True
        return False

Reference

  1. https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/

0 人点赞