Skip to content

Commit f0e6e94

Browse files
add 1091
1 parent a9fe086 commit f0e6e94

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ _If you like this project, please leave me a star._ ★
289289
|1100|[Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1100.java) | |Medium|String, Sliding Window|
290290
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
291291
|1090|[Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1090.java) | [:tv:](https://youtu.be/E0OkE3G95vU)|Medium|HashTable, Greedy|
292+
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1091.java) | |Medium|BFS|
292293
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | |Easy||
293294
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1087.java) | |Medium|Backtracking|
294295
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc)|Easy||
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class _1091 {
7+
public static class Solution1 {
8+
int[] directions = new int[]{0, 1, 1, 0, -1, 1, -1, -1, 0};
9+
10+
public int shortestPathBinaryMatrix(int[][] grid) {
11+
int m = grid.length;
12+
int n = grid[0].length;
13+
if (grid[0][0] == 1 || grid[m - 1][n - 1] == 1) {
14+
return -1;
15+
}
16+
int minPath = 0;
17+
Queue<int[]> queue = new LinkedList<>();
18+
queue.offer(new int[]{0, 0});
19+
boolean[][] visited = new boolean[m][n];
20+
visited[0][0] = true;
21+
while (!queue.isEmpty()) {
22+
int size = queue.size();
23+
for (int i = 0; i < size; i++) {
24+
int[] curr = queue.poll();
25+
if (curr[0] == m - 1 && curr[1] == n - 1) {
26+
return minPath + 1;
27+
}
28+
for (int j = 0; j < directions.length - 1; j++) {
29+
int newx = directions[j] + curr[0];
30+
int newy = directions[j + 1] + curr[1];
31+
if (newx >= 0 && newx < n && newy >= 0 && newy < n && !visited[newx][newy] && grid[newx][newy] == 0) {
32+
queue.offer(new int[]{newx, newy});
33+
visited[newx][newy] = true;
34+
}
35+
}
36+
}
37+
minPath++;
38+
}
39+
return -1;
40+
}
41+
}
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1091;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1091Test {
11+
private static _1091.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1091.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(2, solution1.shortestPathBinaryMatrix(new int[][]{
21+
{0, 1},
22+
{1, 0}
23+
}));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertEquals(4, solution1.shortestPathBinaryMatrix(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0,0],[1,1,0],[1,1,0]")));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(-1, solution1.shortestPathBinaryMatrix(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,0,0],[1,1,0],[1,1,0]")));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
assertEquals(-1, solution1.shortestPathBinaryMatrix(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0,0],[1,1,0],[1,1,1]")));
39+
}
40+
41+
@Test
42+
public void test5() {
43+
assertEquals(1, solution1.shortestPathBinaryMatrix(new int[][]{
44+
{0}
45+
}));
46+
}
47+
48+
@Test
49+
public void test6() {
50+
assertEquals(7, solution1.shortestPathBinaryMatrix(new int[][]{
51+
{0, 1, 0, 0, 1, 1, 0}, {1, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 1, 1, 1, 1}, {0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 1, 0, 0, 0}, {1, 0, 1, 0, 0, 1, 0}
52+
}))
53+
;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)