914. The card packet
Topic links: https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/
Difficulty: Easy
Subject description:
Given a deck of cards, each card are written on an integer.
At this point, you need to select a number X
, so that we can be full decks of cards according to the following rules into 1 组
or 更多组
:
- Each group has a
X
card. - Within the group are written the same integer on all the cards.
Only when you optional X >= 2
returns true
.
Examples
Example 1:
Input: [1,2,3,4,4,3,2,1]
Output: true
interpretation: grouping is possible [1,1], [2,2], [3,3], [ 4,4]
Example 2:
Input: [1,1,1,2,2,2,3,3]
Output: false
interpretation: the packet does not satisfy the requirements.
Example 3:
Input: [1]
Output: false
interpretation: the packet does not satisfy the requirements.
Example 4:
Input: [1,1]
Output: true
interpretation: grouping is possible [1,1]
Example 5:
Input: [1,1,2,2,2,2]
Output: true
interpretation: grouping is possible [1,1], [2,2], [2,2]
prompt:
1 <= deck.length <= 10000
0 <= deck[i] < 10000
Problem-solving ideas
This question I think for a long Leng Shimo made, or road simple questions, self-confidence is rolling, and actually did not expect to use 最大公约数
to do, hey, hum
This question is crucial is that we can think of using 最大公约数
to calculate.
-
1, the first statistics of the number of each element, consisting of a list
- Simple way:
num = list(collections.Counter(deck).values())
one step
- Simple way:
-
2, Step 1 list calculated greatest common divisor obtained
- Simple way:
n = reduce(math.gcd, num)
one step
- Simple way:
-
3, if the divisor than or equal to 2, True is returned, otherwise returns False
Wherein the request n
number 最大公约数
, or 最小公倍数
if you want to manually realized, calculated as follows:
- 1> first find the greatest common divisor and least common multiple of the number 2
- 2> and the results of the third number and the least common multiple and the greatest common divisor continued
- 3> The same demand has been, until all numbers are again seeking
The sample code
# 简单方法
import collections
import math
from functools import reduce
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
num = list(collections.Counter(deck).values())
n = reduce(math.gcd,num)
return False if n<2 else True
#该简单方法参考自:
##作者:jutraman
##链接:https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/solution/pythonqia-pai-fen-zu-by-jutraman/
# 复杂一点的,其实就是把上述简单方法调的函数自己手动实现而已
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
# 第1步:统计,结果保存在num列表中
# 简单方法:num = list(collections.Counter(deck).values())
only_ele = [deck[0]]
num = {deck[0]: 1}
for i in deck[1:]:
if i in only_ele:
n = num[i] + 1
num[i] = n
else:
num[i] = 1
only_ele.append(i)
num = list(num.values())
if len(num)==1:
if num[0] > 1:
return True
return False
# 第2步:计算最大公约数n
# 简单方法:n = reduce(math.gcd, num)
n = self.gcd(num[0],num[1])
for i in range(len(num)-2):
n = gcd(n,num[i+2])
# 第3步:判断最大公约数是否大于等于2
return True if n>1 else False
def gcd(self,a,b):# 计算两个数的最大公约数
if a < b:#让a保持是大的数
temp = b
b = a
a = temp
n = 1
i = 1
while i <= b:
if a % i == 0 and b % i == 0:
n = i
i += 1
return n