← Home

914. X of a Kind in a Deck of Cards

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
914

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 914. X of a Kind in a Deck of Cards, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • GCD
  • time: O(N*log^2(N)), gcd's time complexity: O(log^2(N))
  • space: O(N)

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are gcd, hasGroupsSizeX.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N*log^2(N)), gcd's time complexity: O(log^2(N))
  • Space: O(N)

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//GCD
02//time: O(N*log^2(N)), gcd's time complexity: O(log^2(N))
03//space: O(N)
04//Runtime: 16 ms, faster than 98.24% of C++ online submissions for X of a Kind in a Deck of Cards.
05//Memory Usage: 9.8 MB, less than 100.00% of C++ online submissions for X of a Kind in a Deck of Cards.
06
07class Solution {
08public:
09    int gcd(int a, int b){
10        int tmp;
11        if(a < b){
12            tmp = a;
13            a = b;
14            b = tmp;
15        }
16        if(b == 0)return a;
17        return gcd(b, a%b);
18    }
19    
20    bool hasGroupsSizeX(vector<int>& deck) {
21        if(deck.size() < 2) return false;
22        map<int, int> count;
23        for(int e : deck){
24            count[e]++;
25        }
26        
27        int g = 0;
28        for(map<int, int>::iterator it = count.begin(); it != count.end(); it++){
29            g = gcd(g, it->second);
30        }
31        
32        if(g >= 2) return true;
33        return false;
34    }
35};

Cost

Complexity

Time
O(N*log^2(N)), gcd's time complexity: O(log^2(N))
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(N)
Auxiliary state plus the answer structure where the problem requires one.