1. 程式人生 > 實用技巧 >【LeetCode】【Math】X of a Kind in a Deck of Cards

【LeetCode】【Math】X of a Kind in a Deck of Cards

題目:

在一副紙牌中,每張紙牌上都有一個整數。

當且僅當您可以選擇X> = 2並可以將整個卡片組分成1組或多組卡片時,才返回true,其中:

每個小組都有X張卡片。

每組中的所有卡均具有相同的整數。

Example 1:

Input: deck = [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4].

Example 2:

Input: deck = [1,1,1,2,2,2,3,3]
Output: false´
Explanation: No possible partition.

【解法】列舉法

我們將N張卡片分為K堆,每堆X張,所以必有N%X==0。

然後,N張卡片中每個數字有C_i張,分的每堆必有C_i%X==0。

class Solution:
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        count = collections.Counter(deck)
        N = len(deck)
        for X in range(2, N+1):
            if N % X == 0:
                if all(v % X == 0 for
v in count.values()): return True return False
Runtime:236 ms, faster than 10.94% of Python3 online submissions for X of a Kind in a Deck of Cards. Memory Usage:14 MB, less than 68.07% of Python3 online submissions for X of a Kind in a Deck of Cards.

python3 中取消了 range 函式,而把 xrange 函式重新命名為 range,所以現在直接用 range 函式即可。

range 函式說明:range([start,] stop[, step]),根據start與stop指定的範圍以及step設定的步長,生成一個列表。

xrange函式說明:和range 的用法完全相同,但是返回的是一個生成器。

但是要生成很大的數字序列的時候,用xrange會比range效能優很多,因為不需要一上來就開闢一塊很大的記憶體空間,這兩個基本上都是在迴圈的時候用。除非你需要一個列表,不然xrange可以提高效能。(但我在提交的時候,用xrange會報錯,(⊙o⊙)…

all() 函式用於判斷給定的可迭代引數 iterable 中的所有元素是否都為 TRUE,如果是返回 True,否則返回 False。

元素除了是 0、空、None、False 外都算 True。

語法:all(iterable)

iterable為元組或列表

返回值:如果iterable的所有元素不為0、''、False或者iterable為空,all(iterable)返回True,否則返回False;

注意:空元組、空列表返回值為True,這裡要特別注意。

python3的Collections.Counter計數(類似於Java中的map)

在平常刷題的過程中,經常會遇到需要對字串的字母或者是列表中的數字進行計數,對於Java語言來說可以使用map對其進行計數,在python中可以使用Collections.Counter方法進行計數,最常見的是可以對列表中的元素進行計數,並且計數之後是以字典的形式返回結果的,這樣的話對於後面的操作就很方便了,官方提供的api解釋如下: https://docs.python.org/zh-cn/3.8/library/collections.html#collections.Counter Counter方法可以接收列表、元祖、字串為引數對引數中的元素進行計數,比較常見的是對列表中的元素進行計數,計數完了之後可以訪問鍵可以得到具體的值 【解法】 每個數字i有C_i張卡,被分為X堆,則所有i,都滿足C_i%X == 0 因此,如果C_i的最大公約數 g>1,則令X==g滿足題目條件,反之不滿足。
class Solution:
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        from fractions import gcd
     from functools import reduce
     from math import gcd 
vals
= collections.Counter(deck).values() return reduce(gcd, vals) >= 2

Runtime:236 ms, faster than 10.94% of Python3 online submissions for X of a Kind in a Deck of Cards.

Memory Usage:14.2 MB, less than 5.01% of Python3 online submissions for X of a Kind in a Deck of Cards.
class Solution:
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        def gcd(a, b):
            while b: a, b = b, a % b
            return a
        count = collections.Counter(deck).values()
        return reduce(gcd, count) > 1
Runtime:296 ms, faster than 5.64% of Python3 online submissions for X of a Kind in a Deck of Cards. Memory Usage:13.8 MB, less than 97.81% of Python3 online submissions for X of a Kind in a Deck of Cards. gcd()函式:最大公約數求解

reduce() 函式會對引數序列中元素進行累積。

函式將一個數據集合(連結串列,元組等)中的所有資料進行下列操作:用傳給 reduce 中的函式 function(有兩個引數)先對集合中的第 1、2 個元素進行操作,得到的結果再與第三個資料用 function 函式運算,最後得到一個結果。

reduce() 函式語法:

reduce(function, iterable[, initializer])

  • function -- 函式,有兩個引數
  • iterable -- 可迭代物件
  • initializer -- 可選,初始引數

返回函式計算結果。

在 Python3 中,reduce() 函式已經被從全域性名字空間裡移除了,它現在被放置在 functools 模組裡,如果想要使用它,則需要通過引入 functools 模組來呼叫 reduce() 函式:

from functools import reduce

參考:https://blog.csdn.net/qq_39445165/java/article/details/107089834