Skip to content

Commit 219f754

Browse files
solves #1091: Shortest Path in Binary Matrix in java
1 parent 758a9e4 commit 219f754

File tree

3 files changed

+114
-34
lines changed

3 files changed

+114
-34
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@
501501
| 1085 | 🔒 [Sum of Digits in Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number) | | |
502502
| 1086 | 🔒 [High Five](https://leetcode.com/problems/high-five) | | |
503503
| 1089 | [Duplicate Zeroes](https://leetcode.com/problems/duplicate-zeros) | [![Java](assets/java.png)](src/DuplicateZeros.java) | |
504+
| 1091 | [Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix) | [![Java](assets/java.png)](src/ShortestPathInBinaryMatrix.java) | |
504505
| 1099 | 🔒 [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k) | | |
505506
| 1101 | [The Earliest Moment When Everyone Become Friend](https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends) | [![Java](assets/java.png)](src/TheEarliestMomentWhenEveryoneBecomeFriends.java) | |
506507
| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people) | [![Java](assets/java.png)](src/DistributeCandiesToPeople.java) | |

src/HelloWorld.java

+49-34
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,66 @@
11
// T: O(N + E alp(N))
22
// S: O(N)
33

4+
import java.security.cert.Certificate;
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
9+
410
public class HelloWorld {
5-
private static final class DisjointSet {
6-
private final int[] roots, rank;
7-
8-
public DisjointSet(int size) {
9-
roots = new int[size];
10-
rank = new int[size];
11-
for (int i = 0 ; i < size ; i++) {
12-
roots[i] = i;
13-
rank[i] = 1;
14-
}
11+
public static class Node {
12+
public int val;
13+
public Node left;
14+
public Node right;
15+
public Node next;
16+
17+
public Node() {}
18+
19+
public Node(int _val) {
20+
val = _val;
1521
}
1622

17-
public int find(int num) {
18-
if (num == roots[num]) {
19-
return num;
20-
}
21-
return roots[num] = find(roots[num]);
23+
public Node(int _val, Node _left, Node _right, Node _next) {
24+
val = _val;
25+
left = _left;
26+
right = _right;
27+
next = _next;
2228
}
29+
};
2330

24-
public boolean areConnected(int x, int y) {
25-
return find(x) == find(y);
31+
public Node connect(Node root) {
32+
if (root == null) {
33+
return null;
2634
}
2735

28-
public void union(int x, int y) {
29-
final int rootX = find(x), rootY = find(y);
30-
if (rootX == rootY) {
31-
return;
36+
final Queue<Node> queue = new LinkedList<>();
37+
queue.add(root);
38+
queue.add(null);
39+
Node current = null;
40+
41+
while (!queue.isEmpty()) {
42+
final Node node = queue.poll();
43+
if (node == null) {
44+
if (!queue.isEmpty()) {
45+
queue.add(null);
46+
current = null;
47+
}
48+
continue;
3249
}
33-
if (rank[rootX] > rank[rootY]) {
34-
roots[rootY] = rootX;
35-
} else if (rank[rootX] < rank[rootY]) {
36-
roots[rootX] = rootY;
37-
} else {
38-
roots[rootY] = rootX;
39-
rank[rootX]++;
50+
51+
addChildrenToQueue(queue, node);
52+
53+
if (current != null) {
54+
current.next = node;
4055
}
56+
current = node;
4157
}
58+
59+
return root;
4260
}
4361

44-
public boolean validPath(int n, int[][] edges, int source, int destination) {
45-
final DisjointSet disjointSet = new DisjointSet(n);
46-
for (int[] edge : edges) {
47-
disjointSet.union(edge[0], edge[1]);
48-
}
49-
return disjointSet.areConnected(source, destination);
62+
private static void addChildrenToQueue(Queue<Node> queue, Node node) {
63+
if (node.left != null) queue.add(node.left);
64+
if (node.right != null) queue.add(node.right);
5065
}
5166
}

src/ShortestPathInBinaryMatrix.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// https://leetcode.com/problems/shortest-path-in-binary-matrix
2+
// N = total number of vertices
3+
// T: O(N)
4+
// S: O(N)
5+
6+
import java.util.ArrayList;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Queue;
10+
11+
public class ShortestPathInBinaryMatrix {
12+
private static final List<List<Integer>> directions = List.of(
13+
List.of(-1, -1),
14+
List.of(-1, 0),
15+
List.of(-1, 1),
16+
List.of(1, -1),
17+
List.of(1, 0),
18+
List.of(1, 1),
19+
List.of(0, -1),
20+
List.of(0, 1)
21+
);
22+
23+
public int shortestPathBinaryMatrix(int[][] grid) {
24+
final int n = grid.length;
25+
if (grid[0][0] == 1 || grid[n - 1][n - 1] == 1) {
26+
return -1;
27+
}
28+
29+
final Queue<int[]> queue = new LinkedList<>();
30+
queue.add(new int[] {0, 0});
31+
grid[0][0] = 1;
32+
33+
while (!queue.isEmpty()) {
34+
final int[] position = queue.poll();
35+
if (position[0] == n - 1 && position[1] == n - 1) {
36+
return grid[n - 1][n - 1];
37+
}
38+
39+
for (int[] neighbour : getNeighbours(position, grid)) {
40+
queue.add(neighbour);
41+
grid[neighbour[0]][neighbour[1]] = grid[position[0]][position[1]] + 1;
42+
}
43+
}
44+
45+
return -1;
46+
}
47+
48+
private static List<int[]> getNeighbours(int[] position, int[][] grid) {
49+
final List<int[]> result = new ArrayList<>();
50+
for (List<Integer> direction : directions) {
51+
final int row = position[0] + direction.get(0);
52+
final int column = position[1] + direction.get(1);
53+
if (isValid(grid, row, column) && grid[row][column] == 0) {
54+
result.add(new int[] { row, column });
55+
}
56+
}
57+
return result;
58+
}
59+
60+
private static boolean isValid(int[][] grid, int row, int column) {
61+
final int n = grid.length;
62+
return row >= 0 && row < n && column >= 0 && column < n;
63+
}
64+
}

0 commit comments

Comments
 (0)