Skip to content

Add solutions for 1020, 1023, 1025, 1029 #44

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 4 commits into from
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -27,8 +27,12 @@ Your ideas/fixes/algorithms are more than welcome!

| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1029.java)| O(nlogn) | O(1) | |Easy| Array, Sorting |
|1025|[Divisor Game](https://leetcode.com/problems/divisor-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1025.java)| O(1) | O(1) | |Easy| Math |
|1023|[Camelcase Matching](https://leetcode.com/problems/camelcase-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1023.java)| O(mn) | O(n) | |Medium| String |
|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|
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java) | O(n) | O(1) | |Easy|
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | O(n) | O(1) | |Easy|
53 changes: 53 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1020.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.fishercoder.solutions;

/**
* Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)
*
* A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.
*
* Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves.
*/

public class _1020 {
public static class Solution1 {
public void walk(int[][] A, boolean[][] visited, int x, int y) {
if (x >= A.length || x < 0 || y >= A[0].length || y < 0) return;
if (visited[x][y]) return;
if (A[x][y] == 0) return;

visited[x][y] = true;

walk(A, visited, x - 1, y);
walk(A, visited, x, y - 1);
walk(A, visited, x, y + 1);
walk(A, visited, x + 1, y);
}

public int numEnclaves(int[][] A) {
int n = A.length;
int m = A[0].length;
boolean[][] visited = new boolean[n][m];

for (int i = 0; i < n; ++i) {
walk(A, visited, i, 0);
walk(A, visited, i, m - 1);
}

for (int j = 0; j < m; ++j) {
walk(A, visited, 0, j);
walk(A, visited, n - 1, j);
}

int unreachables = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (A[i][j] == 1 && !visited[i][j]) {
++unreachables;
}
}
}

return unreachables;
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1023.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.List;

/*
* A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it
* equals the query. (We may insert each character at any position, and may insert 0 characters.)
*
* Given a list of queries, and a pattern, return an answer list of booleans,
* where answer[i] is true if and only if queries[i] matches the pattern.
*
* Example:
* queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
* Output: [true,false,true,true,false]
*
* queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
* Output: [false,true,false,false,false]
*/

public class _1023 {

public static class Solution1 {
public List<Boolean> camelMatch(String[] queries, String pattern) {
List<Boolean> res = new ArrayList<>();

for (String s : queries) {
res.add(match(s, pattern));
}

return res;
}

public boolean match(String s, String pattern) {
char[] st = s.toCharArray();

int i = 0;
for (char c : pattern.toCharArray()) {
while (i < st.length && st[i] != c) {
if (Character.isUpperCase(st[i])) return false;
++i;
}

if (i == st.length) return false;
++i;
}

while (i < st.length) {
if (Character.isUpperCase(st[i])) return false;
++i;
}

return true;
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1025.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.
*/
public class _1025 {
public static class Solution1 {
public boolean divisorGame(int N) {
return N % 2 == 0;
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1029.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fishercoder.solutions;

import java.util.Arrays;
import java.util.Comparator;

/**
* There are 2N people a company is planning to interview.
* The cost of flying the i-th person to city A is costs[i][0], and
* the cost of flying the i-th person to city B is costs[i][1].
*
* Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.
*/

public class _1029 {
public static class Solution1 {
public int twoCitySchedCost(int[][] costs) {
Arrays.sort(costs, Comparator.comparing((int[] arr) -> arr[0] - arr[1]));
int totalCost = 0;
final int n = costs.length;

for (int i = 0; i < n; ++i) {
// 2i/n = I(i >= n/2), i.e. 1 if true, 0 if false
totalCost += costs[i][2*i/n];
}
return totalCost;
}
}
}
54 changes: 54 additions & 0 deletions src/test/java/com/fishercoder/_1020Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.fishercoder;

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

import static org.junit.Assert.assertEquals;

public class _1020Test {
private static _1020.Solution1 solution1;

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

@Test
public void test1() {
int[][] map = {
{0, 0, 0, 0},
{1, 0, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0}
};

assertEquals(solution1.numEnclaves(map), 3);
}

@Test
public void test2() {
int[][] map = {
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
};

assertEquals(solution1.numEnclaves(map), 0);
}

@Test
public void test3() {
int[][] map = {
{0, 1, 1, 0},
{0, 0, 0, 0},
{1, 0, 1, 0},
{1, 0, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
};

assertEquals(solution1.numEnclaves(map), 3);
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/fishercoder/_1023Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fishercoder;

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

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class _1023Test {
private static _1023.Solution1 solution1;
private String[] input = {"FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"};

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

@Test
public void test1() {
String pattern = "FB";
List<Boolean> expected = Arrays.asList(true, false, true, true, false);
assertEquals(solution1.camelMatch(input, pattern), expected);
}

@Test
public void test2() {
String pattern = "FoBa";
List<Boolean> expected = Arrays.asList(true, false, true, false, false);
assertEquals(solution1.camelMatch(input, pattern), expected);
}

@Test
public void test3() {
String pattern = "FoBaT";
List<Boolean> expected = Arrays.asList(false, true, false, false, false);
assertEquals(solution1.camelMatch(input, pattern), expected);
}
}
47 changes: 47 additions & 0 deletions src/test/java/com/fishercoder/_1025Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.fishercoder;

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

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class _1025Test {
private static _1025.Solution1 solution1;

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

@Test
public void test1() {
// Alice chooses 1, Bob has no moves
assertTrue(solution1.divisorGame(2));
}

@Test
public void test2() {
// Alice can only choose 1, Bob starts with 2 which is a winning position
assertFalse(solution1.divisorGame(3));
}

@Test
public void test3() {
// Since 3 is a losing position, Alice chooses 1
assertTrue(solution1.divisorGame(4));
}

@Test
public void test4() {
// Alice can only choose 1, and 4 is a winning position
assertFalse(solution1.divisorGame(5));
}

@Test
public void test5() {
// Alice can only choose 1, 2 or 3. She will choose 1 since 5 is a losing position
assertTrue(solution1.divisorGame(6));
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/fishercoder/_1029Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fishercoder;

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

import static org.junit.Assert.assertEquals;

public class _1029Test {
private static _1029.Solution1 solution1;

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

@Test
public void test1() {
/*
* The first person goes to city A for a cost of 10.
* The second person goes to city A for a cost of 30.
* The third person goes to city B for a cost of 50.
* The fourth person goes to city B for a cost of 20.

* The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
*/
int[][] costs = {{10, 20}, {30, 200}, {400, 50}, {30, 20}};
assertEquals(solution1.twoCitySchedCost(costs), 110);
}
}