Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
There is a 50 x 50 chessboard with one knight and some pawns on it. You are given two integers kx and ky where (kx, ky) denotes the position of the knight, and a 2D array positions where positions[i] = [xi, yi] denotes the position of the pawns on the chessboard.
Alice and Bob play a turn-based game, where Alice goes first. In each player's turn:
Alice is trying to maximize the sum of the number of moves made by both players until there are no more pawns on the board, whereas Bob tries to minimize them.
Return the maximum total number of moves made during the game that Alice can achieve, assuming both players play optimally.
Note that in one move, a chess knight has eight possible positions it can move to, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
Example 1:
Input: kx = 1, ky = 1, positions = [[0,0]]
Output: 4
Explanation:
The knight takes 4 moves to reach the pawn at (0, 0).
Example 2:
Input: kx = 0, ky = 2, positions = [[1,1],[2,2],[3,3]]
Output: 8
Explanation:
(2, 2) and captures it in two moves: (0, 2) -> (1, 4) -> (2, 2).(3, 3) and captures it in two moves: (2, 2) -> (4, 1) -> (3, 3).(1, 1) and captures it in four moves: (3, 3) -> (4, 1) -> (2, 2) -> (0, 3) -> (1, 1).Example 3:
Input: kx = 0, ky = 0, positions = [[1,2],[2,4]]
Output: 3
Explanation:
(2, 4) and captures it in two moves: (0, 0) -> (1, 2) -> (2, 4). Note that the pawn at (1, 2) is not captured.(1, 2) and captures it in one move: (2, 4) -> (1, 2).Constraints:
0 <= kx, ky <= 491 <= positions.length <= 15positions[i].length == 20 <= positions[i][0], positions[i][1] <= 49positions[i] are unique.positions[i] != [kx, ky] for all 0 <= i < positions.length.Problem summary: There is a 50 x 50 chessboard with one knight and some pawns on it. You are given two integers kx and ky where (kx, ky) denotes the position of the knight, and a 2D array positions where positions[i] = [xi, yi] denotes the position of the pawns on the chessboard. Alice and Bob play a turn-based game, where Alice goes first. In each player's turn: The player selects a pawn that still exists on the board and captures it with the knight in the fewest possible moves. Note that the player can select any pawn, it might not be one that can be captured in the least number of moves. In the process of capturing the selected pawn, the knight may pass other pawns without capturing them. Only the selected pawn can be captured in this turn. Alice is trying to maximize the sum of the number of moves made by both players until there are no more pawns on the board, whereas Bob tries to minimize them.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Bit Manipulation
1 1 [[0,0]]
0 2 [[1,1],[2,2],[3,3]]
0 0 [[1,2],[2,4]]
knight-probability-in-chessboard)check-knight-tour-configuration)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3283: Maximum Number of Moves to Kill All Pawns
class Solution {
private Integer[][][] f;
private Integer[][][] dist;
private int[][] positions;
private final int[] dx = {1, 1, 2, 2, -1, -1, -2, -2};
private final int[] dy = {2, -2, 1, -1, 2, -2, 1, -1};
public int maxMoves(int kx, int ky, int[][] positions) {
int n = positions.length;
final int m = 50;
dist = new Integer[n + 1][m][m];
this.positions = positions;
for (int i = 0; i <= n; ++i) {
int x = i < n ? positions[i][0] : kx;
int y = i < n ? positions[i][1] : ky;
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {x, y});
for (int step = 1; !q.isEmpty(); ++step) {
for (int k = q.size(); k > 0; --k) {
var p = q.poll();
int x1 = p[0], y1 = p[1];
for (int j = 0; j < 8; ++j) {
int x2 = x1 + dx[j], y2 = y1 + dy[j];
if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < m && dist[i][x2][y2] == null) {
dist[i][x2][y2] = step;
q.offer(new int[] {x2, y2});
}
}
}
}
}
f = new Integer[n + 1][1 << n][2];
return dfs(n, (1 << n) - 1, 1);
}
private int dfs(int last, int state, int k) {
if (state == 0) {
return 0;
}
if (f[last][state][k] != null) {
return f[last][state][k];
}
int res = k == 1 ? 0 : Integer.MAX_VALUE;
for (int i = 0; i < positions.length; ++i) {
int x = positions[i][0], y = positions[i][1];
if ((state >> i & 1) == 1) {
int t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y];
res = k == 1 ? Math.max(res, t) : Math.min(res, t);
}
}
return f[last][state][k] = res;
}
}
// Accepted solution for LeetCode #3283: Maximum Number of Moves to Kill All Pawns
func maxMoves(kx int, ky int, positions [][]int) int {
n := len(positions)
const m = 50
dx := []int{1, 1, 2, 2, -1, -1, -2, -2}
dy := []int{2, -2, 1, -1, 2, -2, 1, -1}
dist := make([][][]int, n+1)
for i := range dist {
dist[i] = make([][]int, m)
for j := range dist[i] {
dist[i][j] = make([]int, m)
for k := range dist[i][j] {
dist[i][j][k] = -1
}
}
}
for i := 0; i <= n; i++ {
x := kx
y := ky
if i < n {
x = positions[i][0]
y = positions[i][1]
}
q := [][2]int{[2]int{x, y}}
dist[i][x][y] = 0
for step := 1; len(q) > 0; step++ {
for k := len(q); k > 0; k-- {
p := q[0]
q = q[1:]
x1, y1 := p[0], p[1]
for j := 0; j < 8; j++ {
x2 := x1 + dx[j]
y2 := y1 + dy[j]
if x2 >= 0 && x2 < m && y2 >= 0 && y2 < m && dist[i][x2][y2] == -1 {
dist[i][x2][y2] = step
q = append(q, [2]int{x2, y2})
}
}
}
}
}
f := make([][][]int, n+1)
for i := range f {
f[i] = make([][]int, 1<<n)
for j := range f[i] {
f[i][j] = make([]int, 2)
for k := range f[i][j] {
f[i][j][k] = -1
}
}
}
var dfs func(last, state, k int) int
dfs = func(last, state, k int) int {
if state == 0 {
return 0
}
if f[last][state][k] != -1 {
return f[last][state][k]
}
var res int
if k == 0 {
res = math.MaxInt32
}
for i, p := range positions {
x, y := p[0], p[1]
if (state>>i)&1 == 1 {
t := dfs(i, state^(1<<i), k^1) + dist[last][x][y]
if k == 1 {
res = max(res, t)
} else {
res = min(res, t)
}
}
}
f[last][state][k] = res
return res
}
return dfs(n, (1<<n)-1, 1)
}
# Accepted solution for LeetCode #3283: Maximum Number of Moves to Kill All Pawns
class Solution:
def maxMoves(self, kx: int, ky: int, positions: List[List[int]]) -> int:
@cache
def dfs(last: int, state: int, k: int) -> int:
if state == 0:
return 0
if k:
res = 0
for i, (x, y) in enumerate(positions):
if state >> i & 1:
t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y]
if res < t:
res = t
return res
else:
res = inf
for i, (x, y) in enumerate(positions):
if state >> i & 1:
t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y]
if res > t:
res = t
return res
n = len(positions)
m = 50
dist = [[[-1] * m for _ in range(m)] for _ in range(n + 1)]
dx = [1, 1, 2, 2, -1, -1, -2, -2]
dy = [2, -2, 1, -1, 2, -2, 1, -1]
positions.append([kx, ky])
for i, (x, y) in enumerate(positions):
dist[i][x][y] = 0
q = deque([(x, y)])
step = 0
while q:
step += 1
for _ in range(len(q)):
x1, y1 = q.popleft()
for j in range(8):
x2, y2 = x1 + dx[j], y1 + dy[j]
if 0 <= x2 < m and 0 <= y2 < m and dist[i][x2][y2] == -1:
dist[i][x2][y2] = step
q.append((x2, y2))
ans = dfs(n, (1 << n) - 1, 1)
dfs.cache_clear()
return ans
// Accepted solution for LeetCode #3283: Maximum Number of Moves to Kill All Pawns
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3283: Maximum Number of Moves to Kill All Pawns
// class Solution {
// private Integer[][][] f;
// private Integer[][][] dist;
// private int[][] positions;
// private final int[] dx = {1, 1, 2, 2, -1, -1, -2, -2};
// private final int[] dy = {2, -2, 1, -1, 2, -2, 1, -1};
//
// public int maxMoves(int kx, int ky, int[][] positions) {
// int n = positions.length;
// final int m = 50;
// dist = new Integer[n + 1][m][m];
// this.positions = positions;
// for (int i = 0; i <= n; ++i) {
// int x = i < n ? positions[i][0] : kx;
// int y = i < n ? positions[i][1] : ky;
// Deque<int[]> q = new ArrayDeque<>();
// q.offer(new int[] {x, y});
// for (int step = 1; !q.isEmpty(); ++step) {
// for (int k = q.size(); k > 0; --k) {
// var p = q.poll();
// int x1 = p[0], y1 = p[1];
// for (int j = 0; j < 8; ++j) {
// int x2 = x1 + dx[j], y2 = y1 + dy[j];
// if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < m && dist[i][x2][y2] == null) {
// dist[i][x2][y2] = step;
// q.offer(new int[] {x2, y2});
// }
// }
// }
// }
// }
// f = new Integer[n + 1][1 << n][2];
// return dfs(n, (1 << n) - 1, 1);
// }
//
// private int dfs(int last, int state, int k) {
// if (state == 0) {
// return 0;
// }
// if (f[last][state][k] != null) {
// return f[last][state][k];
// }
// int res = k == 1 ? 0 : Integer.MAX_VALUE;
// for (int i = 0; i < positions.length; ++i) {
// int x = positions[i][0], y = positions[i][1];
// if ((state >> i & 1) == 1) {
// int t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y];
// res = k == 1 ? Math.max(res, t) : Math.min(res, t);
// }
// }
// return f[last][state][k] = res;
// }
// }
// Accepted solution for LeetCode #3283: Maximum Number of Moves to Kill All Pawns
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3283: Maximum Number of Moves to Kill All Pawns
// class Solution {
// private Integer[][][] f;
// private Integer[][][] dist;
// private int[][] positions;
// private final int[] dx = {1, 1, 2, 2, -1, -1, -2, -2};
// private final int[] dy = {2, -2, 1, -1, 2, -2, 1, -1};
//
// public int maxMoves(int kx, int ky, int[][] positions) {
// int n = positions.length;
// final int m = 50;
// dist = new Integer[n + 1][m][m];
// this.positions = positions;
// for (int i = 0; i <= n; ++i) {
// int x = i < n ? positions[i][0] : kx;
// int y = i < n ? positions[i][1] : ky;
// Deque<int[]> q = new ArrayDeque<>();
// q.offer(new int[] {x, y});
// for (int step = 1; !q.isEmpty(); ++step) {
// for (int k = q.size(); k > 0; --k) {
// var p = q.poll();
// int x1 = p[0], y1 = p[1];
// for (int j = 0; j < 8; ++j) {
// int x2 = x1 + dx[j], y2 = y1 + dy[j];
// if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < m && dist[i][x2][y2] == null) {
// dist[i][x2][y2] = step;
// q.offer(new int[] {x2, y2});
// }
// }
// }
// }
// }
// f = new Integer[n + 1][1 << n][2];
// return dfs(n, (1 << n) - 1, 1);
// }
//
// private int dfs(int last, int state, int k) {
// if (state == 0) {
// return 0;
// }
// if (f[last][state][k] != null) {
// return f[last][state][k];
// }
// int res = k == 1 ? 0 : Integer.MAX_VALUE;
// for (int i = 0; i < positions.length; ++i) {
// int x = positions[i][0], y = positions[i][1];
// if ((state >> i & 1) == 1) {
// int t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y];
// res = k == 1 ? Math.max(res, t) : Math.min(res, t);
// }
// }
// return f[last][state][k] = res;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.