↓Skip to main content
  1. LeetCode/

LeetCode 914: X of a Kind in a Deck of Cards

·2 mins· ·
LeetCode Easy Array Hash-Table Math Counting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-25

  • Submissions: Wrong Answer → Runtime Error ×3 → Wrong Answer → Runtime Error → Wrong Answer ×2 → Accepted ×2

Approach
#

Count each card value (Hash Table). Groups of X require X to divide every count, so X is a common divisor; some X >= 2 exists exactly when the GCD of all counts is at least 2 (Number Theory Template 1).

My Attempts
#

This took 10 submissions, step by step toward the GCD:

  • Attempt 1 required equal counts: a single card [2] returned True instead of False. Adding “count >= 2” then broke on different counts sharing a factor: [1, 3, 1, 1, 1, 3] has counts 4 and 2, groupable by 2, yet it returned False.
  • Next, “every count divisible by the smallest count”: counts 4 and 6 returned False, although groups of 2 work.
  • Then “every count shares a factor with the smallest count”: counts 6, 10 and 15 share factors pairwise, but no single X divides all three, so the answer is False; this returned True.
  • Taking the GCD of all counts passed.

Solution
#

class Solution:
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        return reduce(gcd, Counter(deck).values()) >= 2

Complexity
#

Time O(n + k log n), space O(k).

Mistakes to Avoid
#

  • Counts need not be equal: counts [4, 6] work with groups of 2.
  • Dividing by the smallest count is wrong: for 4 and 6, X is 2, not 4.
  • Pairwise GCDs with the smallest count are not enough: 6, 10, 15 share a factor pairwise, but the GCD of all three is 1.

Interview Explanation
#

I count each card; X must divide every count, so I take the GCD of all counts and check it is at least 2. Time O(n + k log n).

Related

LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned
LeetCode 268: Missing Number
·1 min
LeetCode Easy Array Hash-Table Math Binary-Search
LeetCode 268 note: the sum of 0..n minus the array’s sum is the missing number, which may be 0
LeetCode 908: Smallest Range I
·1 min
LeetCode Easy Array Math
LeetCode 908 note: lower the maximum by k and raise the minimum by k; the answer is max(0, max - min - 2k)
LeetCode 892: Surface Area of 3D Shapes
·2 mins
LeetCode Easy Array Math Geometry Matrix
LeetCode 892 note: a stack of v cubes has 4v + 2 faces; subtract 2 × min for each overlap with the stacks above and to the left
LeetCode 883: Projection Area of 3D Shapes
·1 min
LeetCode Easy Array Math Geometry Matrix
LeetCode 883 note: the top view counts nonzero cells, and the front and side views sum each row’s and each column’s maximum
LeetCode 884: Uncommon Words from Two Sentences
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 884 note: count words across both sentences together; those appearing exactly once are the answer