diff --git a/README.md b/README.md index 12b1423b7a..b6d57399d4 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Your ideas/fixes/algorithms are more than welcome! |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| |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| |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| +|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| |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| |900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Medium| |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 diff --git a/src/main/java/com/fishercoder/solutions/_914.java b/src/main/java/com/fishercoder/solutions/_914.java new file mode 100644 index 0000000000..35b8a86c3a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_914.java @@ -0,0 +1,79 @@ +package com.fishercoder.solutions; + +import java.util.HashMap; +import java.util.Map; + +/** + * In a deck of cards, each card has an integer written on it. + * 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: + * Each group has exactly X cards. + * All the cards in each group have the same integer. + * + * Example 1: + * Input: [1,2,3,4,4,3,2,1] + * Output: true + * Explanation: Possible partition [1,1],[2,2],[3,3],[4,4] + * + * Example 2: + * Input: [1,1,1,2,2,2,3,3] + * Output: false + * Explanation: No possible partition. + * + * Example 3: + * Input: [1] + * Output: false + * Explanation: No possible partition. + * + * Example 4: + * Input: [1,1] + * Output: true + * Explanation: Possible partition [1,1] + * + * Example 5: + * Input: [1,1,2,2,2,2] + * Output: true + * Explanation: Possible partition [1,1],[2,2],[2,2] + */ +public class _914 { + public static class Solution1 { + public boolean hasGroupsSizeX(int[] deck) { + //Size too small for partitions + if (deck.length < 2) + return false; + + //Track repetitions of values in deck array + Map mapReps = new HashMap<>(); + for (int card : deck) { + if (!mapReps.containsKey(card)) + mapReps.put(card,1); + else + mapReps.put(card,mapReps.get(card)+1); + } + + //Create array of map values + int num = 0; + int[] arrReps = new int[mapReps.size()]; + for (Map.Entry e : mapReps.entrySet()){ + arrReps[num++] = e.getValue(); + } + + //Find greatest common denominator + num = arrGCD(arrReps, arrReps.length); + + //If gcd of all repetitions is greater than 1, it's partitionable. + return num > 1; + } + + private int gcd(int a, int b){ + return b == 0 ? a : gcd(b, a % b); + } + + private int arrGCD(int[] arr, int n){ + int result = arr[0]; + for (int i = 1; i < n; i++) + result = gcd(arr[i], result); + + return result; + } + } +} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_914Test.java b/src/test/java/com/fishercoder/_914Test.java new file mode 100644 index 0000000000..8056c6f23b --- /dev/null +++ b/src/test/java/com/fishercoder/_914Test.java @@ -0,0 +1,53 @@ +package com.fishercoder; + +import com.fishercoder.solutions._914; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _914Test { + private static _914.Solution1 solution1; + private int[] arr; + + @BeforeClass + public static void setup() { + solution1 = new _914.Solution1(); + } + + @Test + public void test1() { + arr = new int[]{1}; + assertEquals(false, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test2() { + arr = new int[]{1,1}; + assertEquals(true, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test3() { + arr = new int[]{1,1,1,1,2,2,2,2,2,2}; + assertEquals(true, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test4() { + arr = new int[]{1,1,1,2,2,2,3,3}; + assertEquals(false, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test5() { + arr = new int[]{0,0,1,1,1,1,2,2,3,4}; + assertEquals(false, solution1.hasGroupsSizeX(arr)); + } + + @Test + public void test6() { + arr = new int[]{1,2,3,4,4,3,2,1}; + assertEquals(true, solution1.hasGroupsSizeX(arr)); + } +} \ No newline at end of file