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 8fea23e

Browse files
solves knight probability in java
1 parent cb82764 commit 8fea23e

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

β€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@
362362
| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game) | [![Java](assets/java.png)](src/BaseballGame.java) [![Python](assets/python.png)](python/baseball_game.py) | |
363363
| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match) | | |
364364
| 687 | [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path) | | |
365+
| 688 | [Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard) | [![Java](assets/java.png)](src/KnightProbabilityInChessboard.java) | |
365366
| 690 | [Employee Importance](https://leetcode.com/problems/employee-importance) | | |
366367
| 693 | [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits) | [![Java](assets/java.png)](src/BinaryNumberWithAlternatingBits.java) [![Python](assets/python.png)](python/binary_number_with_alternating_bits.py) | |
367368
| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings) | [![Java](assets/java.png)](src/CountBinarySubstrings.java) [![Python](assets/python.png)](python/count_binary_substrings.py) | |

β€Žsrc/KnightProbabilityInChessboard.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// https://leetcode.com/problems/knight-probability-in-chessboard
2+
// T: O(n^2 * k)
3+
// S: O(n^2)
4+
15
import java.util.ArrayList;
26
import java.util.HashMap;
37
import java.util.List;
@@ -13,7 +17,6 @@ static class Position {
1317
this.n = n;
1418
this.row = row;
1519
this.column = column;
16-
1720
}
1821

1922
private void normalize() {
@@ -28,7 +31,7 @@ private void normalize() {
2831

2932
private int normalize(int index) {
3033
if (index >= (n + 1) / 2) {
31-
return n - index;
34+
return n - index - 1;
3235
}
3336
return index;
3437
}
@@ -46,13 +49,6 @@ private Position apply(Movement movement) {
4649
};
4750
}
4851

49-
private boolean isValid() {
50-
return row >= 0
51-
&& row < n
52-
&& column >= 0
53-
&& column < n;
54-
}
55-
5652
@Override
5753
public boolean equals(Object o) {
5854
if (this == o) return true;
@@ -65,14 +61,6 @@ public boolean equals(Object o) {
6561
public int hashCode() {
6662
return 31 * row + column;
6763
}
68-
69-
@Override
70-
public String toString() {
71-
return "Position{" +
72-
"row=" + row +
73-
", column=" + column +
74-
'}';
75-
}
7664
}
7765

7866
private record State(int n, int k, Position position) { }
@@ -88,17 +76,16 @@ private enum Movement {
8876
LEFT_BOTTOM
8977
}
9078

91-
private final Map<Position, List<Movement>> allowedMovements = new HashMap<>();
92-
private final Map<State, Double> results = new HashMap<>();
79+
private static final Map<Position, List<Movement>> allowedMovements = new HashMap<>();
80+
private static final Map<State, Double> results = new HashMap<>();
81+
private static int[][] validMoves;
9382

94-
public double knightProbability(int n, int k, int row, int column) {
83+
public staticdouble knightProbability(int n, int k, int row, int column) {
9584
computeValidMovements(n);
96-
System.out.println(allowedMovements);
9785
return getKnightProbability(n, k, new Position(row, column, n));
9886
}
9987

100-
private double getKnightProbability(int n, int k, Position position) {
101-
if (!position.isValid()) return 0;
88+
private static double getKnightProbability(int n, int k, Position position) {
10289
if (k == 0) return 1;
10390

10491
position.normalize();
@@ -111,26 +98,37 @@ private double getKnightProbability(int n, int k, Position position) {
11198
return answer;
11299
}
113100

114-
int sumOfProbabilities = 0;
101+
double sumOfProbabilities = 0;
115102
for (Movement movement : allowedMovements.get(position)) {
116103
Position newPosition = position.apply(movement);
117104
sumOfProbabilities += getKnightProbability(n, k - 1, newPosition);
118105
}
119-
double answer = ((double) sumOfProbabilities) / 8;
106+
double answer = sumOfProbabilities / 8;
120107
results.put(state, answer);
121108
return answer;
122109
}
123110

124-
private void computeValidMovements(int n) {
111+
private staticvoid computeValidMovements(int n) {
125112
for (int row = 0 ; row < (n + 1) / 2 ; row++) {
126113
for (int column = row ; column < (n + 1) / 2 ; column++) {
127114
Position position = new Position(row, column, n);
128115
allowedMovements.put(position, knightMovementsAt(position));
129116
}
130117
}
118+
fillValidMovesTable(n);
119+
}
120+
121+
private static void fillValidMovesTable(int n) {
122+
final int length = (n + 1) / 2;
123+
validMoves = new int[length][length];
124+
for (int row = 0 ; row < length ; row++) {
125+
for (int column = row ; column < length ; column++) {
126+
validMoves[row][column] = allowedMovements.get(new Position(row, column, n)).size();
127+
}
128+
}
131129
}
132130

133-
private List<Movement> knightMovementsAt(Position position) {
131+
private staticList<Movement> knightMovementsAt(Position position) {
134132
final List<Movement> movements = new ArrayList<>();
135133
if (isValidIndices(position.row - 2, position.column - 1, position.n)) movements.add(Movement.TOP_LEFT);
136134
if (isValidIndices(position.row - 2, position.column + 1, position.n)) movements.add(Movement.TOP_RIGHT);
@@ -143,14 +141,14 @@ private List<Movement> knightMovementsAt(Position position) {
143141
return movements;
144142
}
145143

146-
private boolean isValidIndices(int row, int column, int n) {
144+
private staticboolean isValidIndices(int row, int column, int n) {
147145
return row >= 0
148146
&& row < n
149147
&& column >= 0
150148
&& column < n;
151149
}
152150

153-
private int validMoves(Position position) {
154-
return allowedMovements.get(position).size();
151+
private staticint validMoves(Position position) {
152+
return validMoves[position.row][position.column];
155153
}
156154
}

0 commit comments

Comments
(0)

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