Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f16c03f

Browse files
solves #909: Snakes and Ladders in java
1 parent 64c5bbb commit f16c03f

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

β€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@
453453
| 897 | [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree) | [![Java](assets/java.png)](src/IncreasingOrderSearchTree.java) | |
454454
| 905 | [Sort Array by Parity](https://leetcode.com/problems/sort-array-by-parity) | | |
455455
| 908 | [Smallest Range I](https://leetcode.com/problems/smallest-range-i) | [![Java](assets/java.png)](src/SmallestRangeI.java) | |
456+
| 909 | [Snakes and Ladders](https://leetcode.com/problems/snakes-and-ladders) | [![Java](assets/java.png)](src/SnakesAndLadders.java) | |
456457
| 914 | [X of a kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards) | [![Java](assets/java.png)](src/XOfAKindInADeckOfCards.java) | |
457458
| 917 | [Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters) | [![Java](assets/java.png)](src/ReverseOnlyLetters.java) | |
458459
| 922 | [Sort Array by Parity II](https://leetcode.com/problems/sort-array-by-parity-ii) | [![Java](assets/java.png)](src/SortArrayByParityII.java) | |

β€Žsrc/SnakesAndLadders.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,77 @@
11
// https://leetcode.com/problems/snakes-and-ladders
2-
// T: O()
3-
// S: O()
2+
// T: O(n^2)
3+
// S: O(n^2)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Queue;
412

513
public class SnakesAndLadders {
14+
private record Position(int row, int column) {}
15+
16+
private record Info(int square, int cost) {}
17+
18+
// BFS: T: O(V + E), V = n^2, E = 6n^2 T: O(n^2), S: O(V) = O(n^2)
19+
public int snakesAndLadders(int[][] board) {
20+
final int n = board.length;
21+
final Map<Integer, Position> squareToPosition = createSquaresToPositions(n);
22+
final Queue<Info> queue = new LinkedList<>() {{ add(new Info(1, 0)); }};
23+
final int[] distances = initializeDistances(n);
24+
25+
while (!queue.isEmpty()) {
26+
final Info info = queue.poll();
27+
if (distances[info.square] <= info.cost) {
28+
continue;
29+
}
30+
31+
distances[info.square] = info.cost;
32+
33+
for (int nextSquare : validPositions(board, info.square, squareToPosition)) {
34+
queue.add(new Info(nextSquare, info.cost + 1));
35+
}
36+
}
37+
38+
return distances[n * n] == Integer.MAX_VALUE ? -1 : distances[n * n];
39+
}
40+
41+
// T: O(1), S: O(1)
42+
private static List<Integer> validPositions(int[][] grid, int square, Map<Integer, Position> positions) {
43+
final int n = grid.length;
44+
final List<Integer> result = new ArrayList<>();
45+
for (int i = 1 ; i <= 6 ; i++) {
46+
final int nextSquare = square + i;
47+
if (nextSquare > n * n) {
48+
break;
49+
}
50+
final Position position = positions.get(nextSquare);
51+
if (grid[position.row][position.column] == -1) {
52+
result.add(nextSquare);
53+
} else {
54+
result.add(grid[position.row][position.column]);
55+
}
56+
}
57+
return result;
58+
}
59+
60+
// T: O(n^2), S: O(n^2)
61+
private static int[] initializeDistances(int n) {
62+
final int[] distances = new int[n * n + 1];
63+
Arrays.fill(distances, Integer.MAX_VALUE);
64+
return distances;
65+
}
66+
67+
// T: O(n^2), S: O(n^2)
68+
private static Map<Integer, Position> createSquaresToPositions(int n) {
69+
final Map<Integer, Position> result = new HashMap<>();
70+
for (int i = 1 ; i <= n * n ; i++) {
71+
final int row = n - ((i - 1) / n) - 1;
72+
final int column = (n - 1 - row) % 2 == 0 ? (i - 1) % n : n - 1 - ((i - 1) % n);
73+
result.put(i, new Position(row, column));
74+
}
75+
return result;
76+
}
677
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /