Skip to content

Commit c4b56e2

Browse files
havanagrawalfishercoder1534
authored andcommitted
Add solution for 1020 (#45)
1 parent 0c191fe commit c4b56e2

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|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|
3131
|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|
32+
|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|
3233
|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|
3334
|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|
3435
|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|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)
5+
*
6+
* A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.
7+
*
8+
* 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.
9+
*/
10+
11+
public class _1020 {
12+
public static class Solution1 {
13+
public void walk(int[][] A, boolean[][] visited, int x, int y) {
14+
if (x >= A.length || x < 0 || y >= A[0].length || y < 0) return;
15+
if (visited[x][y]) return;
16+
if (A[x][y] == 0) return;
17+
18+
visited[x][y] = true;
19+
20+
walk(A, visited, x - 1, y);
21+
walk(A, visited, x, y - 1);
22+
walk(A, visited, x, y + 1);
23+
walk(A, visited, x + 1, y);
24+
}
25+
26+
public int numEnclaves(int[][] A) {
27+
int n = A.length;
28+
int m = A[0].length;
29+
boolean[][] visited = new boolean[n][m];
30+
31+
for (int i = 0; i < n; ++i) {
32+
walk(A, visited, i, 0);
33+
walk(A, visited, i, m - 1);
34+
}
35+
36+
for (int j = 0; j < m; ++j) {
37+
walk(A, visited, 0, j);
38+
walk(A, visited, n - 1, j);
39+
}
40+
41+
int unreachables = 0;
42+
for (int i = 0; i < n; ++i) {
43+
for (int j = 0; j < m; ++j) {
44+
if (A[i][j] == 1 && !visited[i][j]) {
45+
++unreachables;
46+
}
47+
}
48+
}
49+
50+
return unreachables;
51+
}
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1020;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1020Test {
10+
private static _1020.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1020.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
int[][] map = {
20+
{0, 0, 0, 0},
21+
{1, 0, 1, 0},
22+
{0, 1, 1, 0},
23+
{0, 0, 0, 0}
24+
};
25+
26+
assertEquals(solution1.numEnclaves(map), 3);
27+
}
28+
29+
@Test
30+
public void test2() {
31+
int[][] map = {
32+
{0, 1, 1, 0},
33+
{0, 0, 1, 0},
34+
{0, 0, 1, 0},
35+
{0, 0, 0, 0}
36+
};
37+
38+
assertEquals(solution1.numEnclaves(map), 0);
39+
}
40+
41+
@Test
42+
public void test3() {
43+
int[][] map = {
44+
{0, 1, 1, 0},
45+
{0, 0, 0, 0},
46+
{1, 0, 1, 0},
47+
{1, 0, 0, 0},
48+
{0, 1, 1, 0},
49+
{0, 0, 0, 0},
50+
};
51+
52+
assertEquals(solution1.numEnclaves(map), 3);
53+
}
54+
}

0 commit comments

Comments
 (0)