Skip to content

Commit 111be2d

Browse files
authored
Merge branch 'master' into 1023
2 parents 48d8023 + 3a45e16 commit 111be2d

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|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|
3031
|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 |
3132
|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|
3233
|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|
34+
|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|
3335
|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|
3436
|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|
3537
|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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1020. Number of Enclaves
5+
*
6+
* Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)
7+
*
8+
* A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.
9+
*
10+
* 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.
11+
*/
12+
13+
public class _1020 {
14+
public static class Solution1 {
15+
public void walk(int[][] A, boolean[][] visited, int x, int y) {
16+
if (x >= A.length || x < 0 || y >= A[0].length || y < 0) {
17+
return;
18+
}
19+
if (visited[x][y]) {
20+
return;
21+
}
22+
if (A[x][y] == 0) {
23+
return;
24+
}
25+
26+
visited[x][y] = true;
27+
28+
walk(A, visited, x - 1, y);
29+
walk(A, visited, x, y - 1);
30+
walk(A, visited, x, y + 1);
31+
walk(A, visited, x + 1, y);
32+
}
33+
34+
public int numEnclaves(int[][] A) {
35+
int n = A.length;
36+
int m = A[0].length;
37+
boolean[][] visited = new boolean[n][m];
38+
39+
for (int i = 0; i < n; ++i) {
40+
walk(A, visited, i, 0);
41+
walk(A, visited, i, m - 1);
42+
}
43+
44+
for (int j = 0; j < m; ++j) {
45+
walk(A, visited, 0, j);
46+
walk(A, visited, n - 1, j);
47+
}
48+
49+
int unreachables = 0;
50+
for (int i = 0; i < n; ++i) {
51+
for (int j = 0; j < m; ++j) {
52+
if (A[i][j] == 1 && !visited[i][j]) {
53+
++unreachables;
54+
}
55+
}
56+
}
57+
58+
return unreachables;
59+
}
60+
}
61+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
/**
9+
* 1030. Matrix Cells in Distance Order
10+
*
11+
* We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.
12+
*
13+
* Additionally, we are given a cell in that matrix with coordinates (r0, c0).
14+
*
15+
* Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)
16+
*
17+
* Example 1:
18+
* Input: R = 1, C = 2, r0 = 0, c0 = 0
19+
* Output: [[0,0],[0,1]]
20+
* Explanation: The distances from (r0, c0) to other cells are: [0,1]
21+
*
22+
* Example 2:
23+
* Input: R = 2, C = 2, r0 = 0, c0 = 1
24+
* Output: [[0,1],[0,0],[1,1],[1,0]]
25+
* Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
26+
* The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
27+
*
28+
* Example 3:
29+
* Input: R = 2, C = 3, r0 = 1, c0 = 2
30+
* Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
31+
* Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
32+
* There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
33+
*
34+
* Note:
35+
*
36+
* 1 <= R <= 100
37+
* 1 <= C <= 100
38+
* 0 <= r0 < R
39+
* 0 <= c0 < C
40+
* */
41+
public class _1030 {
42+
public static class Solution1 {
43+
public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
44+
int[][] result = new int[R * C][2];
45+
Queue<int[]> queue = new LinkedList<>();
46+
queue.offer(new int[]{r0, c0});
47+
boolean[][] visited = new boolean[R][C];
48+
int i = 0;
49+
while (!queue.isEmpty()) {
50+
int[] cell = queue.poll();
51+
int row = cell[0];
52+
int col = cell[1];
53+
54+
if (row < 0 || row >= R || col < 0 || col >= C || visited[row][col]) {
55+
continue;
56+
}
57+
58+
visited[row][col] = true;
59+
result[i++] = new int[]{row, col};
60+
queue.offer(new int[]{row, col + 1});
61+
queue.offer(new int[]{row + 1, col});
62+
queue.offer(new int[]{row - 1, col});
63+
queue.offer(new int[]{row, col - 1});
64+
65+
}
66+
return result;
67+
}
68+
}
69+
}
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1030;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _1030Test {
9+
private static _1030.Solution1 solution1;
10+
11+
@BeforeClass
12+
public static void setup() {
13+
solution1 = new _1030.Solution1();
14+
}
15+
16+
@Test
17+
public void test1() {
18+
CommonUtils.print2DIntArray(solution1.allCellsDistOrder(1, 2, 0, 0));
19+
}
20+
21+
@Test
22+
public void test2() {
23+
CommonUtils.print2DIntArray(solution1.allCellsDistOrder(2, 2, 0, 1));
24+
}
25+
26+
@Test
27+
public void test3() {
28+
CommonUtils.print2DIntArray(solution1.allCellsDistOrder(2, 3, 1, 2));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)