Skip to content

Commit 458d454

Browse files
west-dBestfishercoder1534
authored andcommitted
Added p914 (#56)
1 parent b9c87ca commit 458d454

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Your ideas/fixes/algorithms are more than welcome!
6464
|925|[Long Pressed Name](https://leetcode.com/problems/long-pressed-name/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_925.java) | O(n) | O(1) | |Easy|
6565
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | O(n) | O(1) | |Easy|
6666
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | O(n) | O(n) | |Easy|
67+
|914|[X of a Kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_914.java) | O(n) | O(n) | |Easy|
6768
|908|[Smallest Range I](https://leetcode.com/problems/smallest-range-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_908.java) | O(n) | O(1) | |Easy|
6869
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Medium|
6970
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | O(n) | O(n) | |Easy| DFS, recursion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* In a deck of cards, each card has an integer written on it.
8+
* 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+
*
12+
* Example 1:
13+
* Input: [1,2,3,4,4,3,2,1]
14+
* Output: true
15+
* Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]
16+
*
17+
* Example 2:
18+
* Input: [1,1,1,2,2,2,3,3]
19+
* Output: false
20+
* Explanation: No possible partition.
21+
*
22+
* Example 3:
23+
* Input: [1]
24+
* Output: false
25+
* Explanation: No possible partition.
26+
*
27+
* Example 4:
28+
* Input: [1,1]
29+
* Output: true
30+
* Explanation: Possible partition [1,1]
31+
*
32+
* Example 5:
33+
* Input: [1,1,2,2,2,2]
34+
* Output: true
35+
* Explanation: Possible partition [1,1],[2,2],[2,2]
36+
*/
37+
public class _914 {
38+
public static class Solution1 {
39+
public boolean hasGroupsSizeX(int[] deck) {
40+
//Size too small for partitions
41+
if (deck.length < 2)
42+
return false;
43+
44+
//Track repetitions of values in deck array
45+
Map<Integer, Integer> mapReps = new HashMap<>();
46+
for (int card : deck) {
47+
if (!mapReps.containsKey(card))
48+
mapReps.put(card,1);
49+
else
50+
mapReps.put(card,mapReps.get(card)+1);
51+
}
52+
53+
//Create array of map values
54+
int num = 0;
55+
int[] arrReps = new int[mapReps.size()];
56+
for (Map.Entry<Integer,Integer> e : mapReps.entrySet()){
57+
arrReps[num++] = e.getValue();
58+
}
59+
60+
//Find greatest common denominator
61+
num = arrGCD(arrReps, arrReps.length);
62+
63+
//If gcd of all repetitions is greater than 1, it's partitionable.
64+
return num > 1;
65+
}
66+
67+
private int gcd(int a, int b){
68+
return b == 0 ? a : gcd(b, a % b);
69+
}
70+
71+
private int arrGCD(int[] arr, int n){
72+
int result = arr[0];
73+
for (int i = 1; i < n; i++)
74+
result = gcd(arr[i], result);
75+
76+
return result;
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._914;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _914Test {
10+
private static _914.Solution1 solution1;
11+
private int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _914.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{1};
21+
assertEquals(false, solution1.hasGroupsSizeX(arr));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
arr = new int[]{1,1};
27+
assertEquals(true, solution1.hasGroupsSizeX(arr));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
arr = new int[]{1,1,1,1,2,2,2,2,2,2};
33+
assertEquals(true, solution1.hasGroupsSizeX(arr));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
arr = new int[]{1,1,1,2,2,2,3,3};
39+
assertEquals(false, solution1.hasGroupsSizeX(arr));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
arr = new int[]{0,0,1,1,1,1,2,2,3,4};
45+
assertEquals(false, solution1.hasGroupsSizeX(arr));
46+
}
47+
48+
@Test
49+
public void test6() {
50+
arr = new int[]{1,2,3,4,4,3,2,1};
51+
assertEquals(true, solution1.hasGroupsSizeX(arr));
52+
}
53+
}

0 commit comments

Comments
 (0)