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

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

1. Description

X of a Kind in a Deck of Cards

2. Solution

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

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
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/
如果有收获,可以请我喝杯咖啡!