Skip to content

Added p914 #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/com/fishercoder/solutions/_914.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Integer,Integer> 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;
}
}
}
53 changes: 53 additions & 0 deletions src/test/java/com/fishercoder/_914Test.java
Original file line number Diff line number Diff line change
@@ -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));
}
}