Skip to content

Commit 3a45e16

Browse files
add 1030
1 parent 85dffcf commit 3a45e16

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ 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
|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|
3132
|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|
3233
|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|
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: 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)