4
4
import java .util .Map ;
5
5
6
6
/**
7
+ * 914. X of a Kind in a Deck of Cards
8
+ *
7
9
* In a deck of cards, each card has an integer written on it.
8
10
* Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:
9
- * Each group has exactly X cards.
10
- * All the cards in each group have the same integer.
11
+ * Each group has exactly X cards.
12
+ * All the cards in each group have the same integer.
11
13
*
12
14
* Example 1:
13
15
* Input: [1,2,3,4,4,3,2,1]
@@ -38,22 +40,24 @@ public class _914 {
38
40
public static class Solution1 {
39
41
public boolean hasGroupsSizeX (int [] deck ) {
40
42
//Size too small for partitions
41
- if (deck .length < 2 )
43
+ if (deck .length < 2 ) {
42
44
return false ;
45
+ }
43
46
44
47
//Track repetitions of values in deck array
45
48
Map <Integer , Integer > mapReps = new HashMap <>();
46
49
for (int card : deck ) {
47
- if (!mapReps .containsKey (card ))
48
- mapReps .put (card ,1 );
49
- else
50
- mapReps .put (card ,mapReps .get (card )+1 );
50
+ if (!mapReps .containsKey (card )) {
51
+ mapReps .put (card , 1 );
52
+ } else {
53
+ mapReps .put (card , mapReps .get (card ) + 1 );
54
+ }
51
55
}
52
56
53
57
//Create array of map values
54
58
int num = 0 ;
55
59
int [] arrReps = new int [mapReps .size ()];
56
- for (Map .Entry <Integer ,Integer > e : mapReps .entrySet ()){
60
+ for (Map .Entry <Integer , Integer > e : mapReps .entrySet ()) {
57
61
arrReps [num ++] = e .getValue ();
58
62
}
59
63
@@ -64,14 +68,15 @@ public boolean hasGroupsSizeX(int[] deck) {
64
68
return num > 1 ;
65
69
}
66
70
67
- private int gcd (int a , int b ){
71
+ private int gcd (int a , int b ) {
68
72
return b == 0 ? a : gcd (b , a % b );
69
73
}
70
74
71
- private int arrGCD (int [] arr , int n ){
75
+ private int arrGCD (int [] arr , int n ) {
72
76
int result = arr [0 ];
73
- for (int i = 1 ; i < n ; i ++)
77
+ for (int i = 1 ; i < n ; i ++) {
74
78
result = gcd (arr [i ], result );
79
+ }
75
80
76
81
return result ;
77
82
}
0 commit comments