Skip to content

Commit 432c5c5

Browse files
committed
Runtime: 17 ms (Top 78.04%) | Memory: 46.7 MB (Top 69.44%)
1 parent eac2035 commit 432c5c5

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

scripts/algorithms/S/Shortest Path Visiting All Nodes/Shortest Path Visiting All Nodes.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 17 ms (Top 78.04%) | Memory: 46.7 MB (Top 69.44%)
12
class Solution {
23
class Pair {
34
int i;
@@ -13,15 +14,15 @@ public int shortestPathLength(int[][] graph) {
1314
boolean[currentNode][steps]
1415
*/
1516
int n = graph.length;
16-
17+
1718
// 111....1, 1<< n - 1
1819
int allVisited = (1 << n) - 1;
19-
20+
2021
boolean[][] visited = new boolean[n][1 << n];
2122
Queue<Pair> q = new LinkedList<>();
2223
for (int i = 0; i < n; i++) {
2324
if (1 << i == allVisited) return 0;
24-
visited[i][1 << i] = true;
25+
visited[i][1 << i] = true;
2526
q.offer(new Pair(i, 1 << i));
2627
}
2728
int step = 0;
@@ -30,18 +31,18 @@ public int shortestPathLength(int[][] graph) {
3031
for (int i = 0; i < size; i++) {
3132
Pair p = q.poll();
3233
int[] edges = graph[p.i];
33-
34+
3435
for(int t: edges) {
3536
int path = p.path | (1 << t);
3637
if (path == allVisited) return step + 1;
3738
if (!visited[t][path]) {
3839
visited[t][path] = true;
39-
q.offer(new Pair(t, path));
40-
}
40+
q.offer(new Pair(t, path));
41+
}
4142
}
4243
}
4344
step++;
4445
}
4546
return step;
4647
}
47-
}
48+
}

0 commit comments

Comments
 (0)