Skip to content

Adding 1025 #58

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Your ideas/fixes/algorithms are more than welcome!
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | O(R*C) | O(1) | |Easy|
|1025|[Divisor Game](https://leetcode.com/problems/divisor-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1025.java) | O(1) | O(1) | |Easy|
|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | O(n) | O(n) | |Easy|
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy|
|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java) | O(mn) | O(mn) | |Medium|Graph, DFS, BFS, recursion|
Expand Down Expand Up @@ -63,6 +64,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
28 changes: 28 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1025.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fishercoder.solutions;

/**
* Alice and Bob take turns playing a game, with Alice starting first.
* Initially, there is a number N on the chalkboard. On each player's turn, that player makes a move consisting of:
* Choosing any x with 0 < x < N and N % x == 0.
* Replacing the number N on the chalkboard with N - x.
* Also, if a player cannot make a move, they lose the game.
* Return True if and only if Alice wins the game, assuming both players play optimally.
*
*
* Example 1:
* Input: 2
* Output: true
* Explanation: Alice chooses 1, and Bob has no more moves.
*
* Example 2:
* Input: 3
* Output: false
* Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
*/
public class _1025 {
public static class Solution1 {
public boolean divisorGame(int N) {
return N % 2 == 0;
}
}
}
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;
}
}
}
40 changes: 40 additions & 0 deletions src/test/java/com/fishercoder/_1025Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fishercoder;

import com.fishercoder.solutions._1025;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Random;

import static org.junit.Assert.assertEquals;

public class _1025Test {
private static _1025.Solution1 solution1;
Random rdm = new Random();
int RANGE = 256; //can be any number, so long as 2*RANGE + 1 is within bounds

@BeforeClass
public static void setup() {
solution1 = new _1025.Solution1();
}

@Test
public void test1() {
assertEquals(false, solution1.divisorGame(1));
}

@Test
public void test2() {
assertEquals(true, solution1.divisorGame(2));
}

@Test
public void test3() {
assertEquals(true, solution1.divisorGame(2*rdm.nextInt(RANGE)));
}

@Test
public void test4() {
assertEquals(false, solution1.divisorGame(2*rdm.nextInt(RANGE)+1));
}
}
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));
}
}