Skip to content

Commit e0cfb1b

Browse files
Course Schedule
Shortest Path in Binary Matrix
1 parent a7f282d commit e0cfb1b

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode.medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
public class CourseSchedule {
9+
10+
// evaluate using BFS
11+
boolean canFinish(int numCourses, int[][] prerequisites) {
12+
List<List<Integer>> graph = new ArrayList<>();
13+
int[] inDegree = new int[numCourses];
14+
for (int i = 0; i < numCourses; i++)
15+
graph.add(new ArrayList<>());
16+
17+
18+
for (int[] pre : prerequisites) {
19+
graph.get(pre[1]).add(pre[0]);
20+
inDegree[pre[0]]++;
21+
}
22+
23+
Queue<Integer> queue = new LinkedList<>();
24+
for (int i = 0; i < numCourses; i++)
25+
if (inDegree[i] == 0)
26+
queue.offer(i);
27+
28+
int count = 0;
29+
while (!queue.isEmpty()) {
30+
int curr = queue.poll();
31+
count++;
32+
for (int next : graph.get(curr)) {
33+
inDegree[next]--;
34+
if (inDegree[next] == 0) queue.offer(next);
35+
}
36+
}
37+
return count == numCourses;
38+
}
39+
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode.medium;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class ShortestPathInBinaryMatrix {
7+
8+
int shortestPathBinaryMatrix(int[][] grid) {
9+
int n = grid.length;
10+
11+
if (grid[0][0] != 0 || grid[n - 1][n - 1] != 0)
12+
return -1;
13+
14+
int[][] dirs = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
15+
16+
Queue<int[]> queue = new LinkedList<>();
17+
18+
queue.offer(new int[]{0, 0, 1}); // row, col, path length
19+
20+
boolean[][] visited = new boolean[n][n];
21+
visited[0][0] = true;
22+
while (!queue.isEmpty()) {
23+
int[] curr = queue.poll();
24+
int r = curr[0], c = curr[1], len = curr[2];
25+
if (r == n - 1 && c == n - 1) return len;
26+
for (int[] d : dirs) {
27+
int nr = r + d[0], nc = c + d[1];
28+
if (nr >= 0 && nr < n && nc >= 0 && nc < n && grid[nr][nc] == 0 && !visited[nr][nc]) {
29+
queue.offer(new int[]{nr, nc, len + 1});
30+
visited[nr][nc] = true;
31+
}
32+
}
33+
}
34+
35+
return -1;
36+
}
37+
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode.medium;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class CourseScheduleTest {
8+
9+
private final CourseSchedule courseSchedule;
10+
11+
CourseScheduleTest() {
12+
courseSchedule = new CourseSchedule();
13+
}
14+
15+
@Test
16+
void testCanFinish1() {
17+
int numCourses = 2;
18+
int[][] prerequisites = {{1, 0}};
19+
assertTrue(courseSchedule.canFinish(numCourses, prerequisites));
20+
}
21+
22+
@Test
23+
void testCanFinish2() {
24+
int numCourses = 2;
25+
int[][] prerequisites = {{1, 0}, {0, 1}};
26+
assertFalse(courseSchedule.canFinish(numCourses, prerequisites));
27+
}
28+
29+
@Test
30+
void testCanFinish3() {
31+
int numCourses = 3;
32+
int[][] prerequisites = {{0, 1}, {0, 2}, {1, 2}};
33+
assertTrue(courseSchedule.canFinish(numCourses, prerequisites));
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode.medium;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ShortestPathInBinaryMatrixTest {
8+
9+
private final ShortestPathInBinaryMatrix shortestPathInBinaryMatrix;
10+
11+
ShortestPathInBinaryMatrixTest() {
12+
shortestPathInBinaryMatrix = new ShortestPathInBinaryMatrix();
13+
}
14+
15+
@Test
16+
void testShortestPathBinaryMatrix1() {
17+
int[][] grid = {
18+
{0, 1},
19+
{1, 0}
20+
};
21+
assertEquals(2, shortestPathInBinaryMatrix.shortestPathBinaryMatrix(grid));
22+
}
23+
24+
@Test
25+
void testShortestPathBinaryMatrix2() {
26+
int[][] grid = {
27+
{0, 0, 0},
28+
{1, 1, 0},
29+
{1, 1, 0}
30+
};
31+
assertEquals(4, shortestPathInBinaryMatrix.shortestPathBinaryMatrix(grid));
32+
}
33+
}

0 commit comments

Comments
 (0)