Skip to content

Add solution for 1020 #45

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 21, 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 @@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
|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|
Expand Down
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;
}
}
}
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);
}
}