diff --git a/README.md b/README.md index 079377933c..9f27fc5088 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/src/main/java/com/fishercoder/solutions/_1020.java b/src/main/java/com/fishercoder/solutions/_1020.java new file mode 100644 index 0000000000..eec5f59fc8 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_1020.java @@ -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; + } + } +} diff --git a/src/test/java/com/fishercoder/_1020Test.java b/src/test/java/com/fishercoder/_1020Test.java new file mode 100644 index 0000000000..359557c6c3 --- /dev/null +++ b/src/test/java/com/fishercoder/_1020Test.java @@ -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); + } +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: