-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeckPartition.java
More file actions
33 lines (28 loc) · 916 Bytes
/
DeckPartition.java
File metadata and controls
33 lines (28 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards
public class DeckPartition {
public boolean hasGroupsSizeX(int[] deck) {
int[] size = new int[10000];
for (int num : deck)
size[num]++;
int gcd = size[deck[0]];
for (int num : deck) {
gcd = GCD( size[num], gcd);
}
return (gcd != 1);
}
private static int GCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
DeckPartition dp = new DeckPartition();
int[] deck = new int[]{1,2,3,4,4,3,2,1};
System.out.println(dp.hasGroupsSizeX(deck));
deck = new int[]{1,1,1,2,2,2,3,3};
System.out.println(dp.hasGroupsSizeX(deck));
}
}