diff --git "a/leetcode3/\354\227\274\355\230\234\354\240\225/914. X of a Kind in a Deck of Cards.java" "b/leetcode3/\354\227\274\355\230\234\354\240\225/914. X of a Kind in a Deck of Cards.java" new file mode 100644 index 00000000..9e710a78 --- /dev/null +++ "b/leetcode3/\354\227\274\355\230\234\354\240\225/914. X of a Kind in a Deck of Cards.java" @@ -0,0 +1,25 @@ +// O(nlogn) + +import java.util.Arrays; + +class Solution { + public boolean hasGroupsSizeX(int[] deck) { + Arrays.sort(deck); + + int g = 0; + int count = 1; + for (int i = 1; i <= deck.length; i++) { + if (i < deck.length && deck[i] == deck[i - 1]) { + count++; + } else { + g = gcd(g, count); + count = 1; + } + } + return g >= 2; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } +}