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.
You are given a m x n 2D array board representing a chessboard, where board[i][j] represents the value of the cell (i, j).
Rooks in the same row or column attack each other. You need to place three rooks on the chessboard such that the rooks do not attack each other.
Return the maximum sum of the cell values on which the rooks are placed.
Example 1:
Input: board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
Output: 4
Explanation:
We can place the rooks in the cells (0, 2), (1, 3), and (2, 1) for a sum of 1 + 1 + 2 = 4.
Example 2:
Input: board = [[1,2,3],[4,5,6],[7,8,9]]
Output: 15
Explanation:
We can place the rooks in the cells (0, 0), (1, 1), and (2, 2) for a sum of 1 + 5 + 9 = 15.
Example 3:
Input: board = [[1,1,1],[1,1,1],[1,1,1]]
Output: 3
Explanation:
We can place the rooks in the cells (0, 2), (1, 1), and (2, 0) for a sum of 1 + 1 + 1 = 3.
Constraints:
3 <= m == board.length <= 1003 <= n == board[i].length <= 100-109 <= board[i][j] <= 109Problem summary: You are given a m x n 2D array board representing a chessboard, where board[i][j] represents the value of the cell (i, j). Rooks in the same row or column attack each other. You need to place three rooks on the chessboard such that the rooks do not attack each other. Return the maximum sum of the cell values on which the rooks are placed.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
[[1,2,3],[4,5,6],[7,8,9]]
[[1,1,1],[1,1,1],[1,1,1]]
available-captures-for-rook)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3256: Maximum Value Sum by Placing Three Rooks I
class Solution {
public long maximumValueSum(int[][] board) {
final int m = board.length;
final int n = board[0].length;
long ans = Long.MIN_VALUE;
List<int[]>[] rows = new ArrayList[m];
List<int[]>[] cols = new ArrayList[n];
Set<int[]> rowSet = new HashSet<>();
Set<int[]> colSet = new HashSet<>();
Set<int[]> boardSet = new HashSet<>();
for (int i = 0; i < m; ++i)
rows[i] = new ArrayList<>();
for (int j = 0; j < n; ++j)
cols[j] = new ArrayList<>();
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
int[] cell = new int[] {board[i][j], i, j};
rows[i].add(cell);
cols[j].add(cell);
}
Comparator<int[]> comparator = Comparator.comparingInt(a -> - a[0]);
for (List<int[]> row : rows) {
row.sort(comparator);
rowSet.addAll(row.subList(0, Math.min(3, row.size())));
}
for (List<int[]> col : cols) {
col.sort(comparator);
colSet.addAll(col.subList(0, Math.min(3, col.size())));
}
boardSet.addAll(rowSet);
boardSet.retainAll(colSet);
// At least 9 positions are required on the board to place 3 rooks such that
// none can attack another.
List<int[]> topNine = new ArrayList<>(boardSet);
topNine.sort(comparator);
topNine = topNine.subList(0, Math.min(9, topNine.size()));
for (int i = 0; i < topNine.size(); ++i)
for (int j = i + 1; j < topNine.size(); ++j)
for (int k = j + 1; k < topNine.size(); ++k) {
int[] t1 = topNine.get(i);
int[] t2 = topNine.get(j);
int[] t3 = topNine.get(k);
if (t1[1] == t2[1] || t1[1] == t3[1] || t2[1] == t3[1] || //
t1[2] == t2[2] || t1[2] == t3[2] || t2[2] == t3[2])
continue;
ans = Math.max(ans, (long) t1[0] + t2[0] + t3[0]);
}
return ans;
}
}
// Accepted solution for LeetCode #3256: Maximum Value Sum by Placing Three Rooks I
// Auto-generated Go example from java.
func exampleSolution() {
}
// Reference (java):
// // Accepted solution for LeetCode #3256: Maximum Value Sum by Placing Three Rooks I
// class Solution {
// public long maximumValueSum(int[][] board) {
// final int m = board.length;
// final int n = board[0].length;
// long ans = Long.MIN_VALUE;
// List<int[]>[] rows = new ArrayList[m];
// List<int[]>[] cols = new ArrayList[n];
// Set<int[]> rowSet = new HashSet<>();
// Set<int[]> colSet = new HashSet<>();
// Set<int[]> boardSet = new HashSet<>();
//
// for (int i = 0; i < m; ++i)
// rows[i] = new ArrayList<>();
//
// for (int j = 0; j < n; ++j)
// cols[j] = new ArrayList<>();
//
// for (int i = 0; i < m; ++i)
// for (int j = 0; j < n; ++j) {
// int[] cell = new int[] {board[i][j], i, j};
// rows[i].add(cell);
// cols[j].add(cell);
// }
//
// Comparator<int[]> comparator = Comparator.comparingInt(a -> - a[0]);
//
// for (List<int[]> row : rows) {
// row.sort(comparator);
// rowSet.addAll(row.subList(0, Math.min(3, row.size())));
// }
//
// for (List<int[]> col : cols) {
// col.sort(comparator);
// colSet.addAll(col.subList(0, Math.min(3, col.size())));
// }
//
// boardSet.addAll(rowSet);
// boardSet.retainAll(colSet);
//
// // At least 9 positions are required on the board to place 3 rooks such that
// // none can attack another.
// List<int[]> topNine = new ArrayList<>(boardSet);
// topNine.sort(comparator);
// topNine = topNine.subList(0, Math.min(9, topNine.size()));
//
// for (int i = 0; i < topNine.size(); ++i)
// for (int j = i + 1; j < topNine.size(); ++j)
// for (int k = j + 1; k < topNine.size(); ++k) {
// int[] t1 = topNine.get(i);
// int[] t2 = topNine.get(j);
// int[] t3 = topNine.get(k);
// if (t1[1] == t2[1] || t1[1] == t3[1] || t2[1] == t3[1] || //
// t1[2] == t2[2] || t1[2] == t3[2] || t2[2] == t3[2])
// continue;
// ans = Math.max(ans, (long) t1[0] + t2[0] + t3[0]);
// }
//
// return ans;
// }
// }
# Accepted solution for LeetCode #3256: Maximum Value Sum by Placing Three Rooks I
class Solution:
def maximumValueSum(self, board: list[list[int]]) -> int:
rows = [heapq.nlargest(3, [(val, i, j)
for j, val in enumerate(row)])
for i, row in enumerate(board)]
cols = [heapq.nlargest(3, [(val, i, j)
for i, val in enumerate(col)])
for j, col in enumerate(zip(*board))]
topNine = heapq.nlargest(9,
set(itertools.chain(*rows)) &
set(itertools.chain(*cols)))
return max(
(val1 + val2 + val3 for
(val1, i1, j1),
(val2, i2, j2),
(val3, i3, j3) in (itertools.combinations(topNine, 3))
if len({i1, i2, i3}) == 3 and len({j1, j2, j3}) == 3))
// Accepted solution for LeetCode #3256: Maximum Value Sum by Placing Three Rooks I
// 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 #3256: Maximum Value Sum by Placing Three Rooks I
// class Solution {
// public long maximumValueSum(int[][] board) {
// final int m = board.length;
// final int n = board[0].length;
// long ans = Long.MIN_VALUE;
// List<int[]>[] rows = new ArrayList[m];
// List<int[]>[] cols = new ArrayList[n];
// Set<int[]> rowSet = new HashSet<>();
// Set<int[]> colSet = new HashSet<>();
// Set<int[]> boardSet = new HashSet<>();
//
// for (int i = 0; i < m; ++i)
// rows[i] = new ArrayList<>();
//
// for (int j = 0; j < n; ++j)
// cols[j] = new ArrayList<>();
//
// for (int i = 0; i < m; ++i)
// for (int j = 0; j < n; ++j) {
// int[] cell = new int[] {board[i][j], i, j};
// rows[i].add(cell);
// cols[j].add(cell);
// }
//
// Comparator<int[]> comparator = Comparator.comparingInt(a -> - a[0]);
//
// for (List<int[]> row : rows) {
// row.sort(comparator);
// rowSet.addAll(row.subList(0, Math.min(3, row.size())));
// }
//
// for (List<int[]> col : cols) {
// col.sort(comparator);
// colSet.addAll(col.subList(0, Math.min(3, col.size())));
// }
//
// boardSet.addAll(rowSet);
// boardSet.retainAll(colSet);
//
// // At least 9 positions are required on the board to place 3 rooks such that
// // none can attack another.
// List<int[]> topNine = new ArrayList<>(boardSet);
// topNine.sort(comparator);
// topNine = topNine.subList(0, Math.min(9, topNine.size()));
//
// for (int i = 0; i < topNine.size(); ++i)
// for (int j = i + 1; j < topNine.size(); ++j)
// for (int k = j + 1; k < topNine.size(); ++k) {
// int[] t1 = topNine.get(i);
// int[] t2 = topNine.get(j);
// int[] t3 = topNine.get(k);
// if (t1[1] == t2[1] || t1[1] == t3[1] || t2[1] == t3[1] || //
// t1[2] == t2[2] || t1[2] == t3[2] || t2[2] == t3[2])
// continue;
// ans = Math.max(ans, (long) t1[0] + t2[0] + t3[0]);
// }
//
// return ans;
// }
// }
// Accepted solution for LeetCode #3256: Maximum Value Sum by Placing Three Rooks I
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3256: Maximum Value Sum by Placing Three Rooks I
// class Solution {
// public long maximumValueSum(int[][] board) {
// final int m = board.length;
// final int n = board[0].length;
// long ans = Long.MIN_VALUE;
// List<int[]>[] rows = new ArrayList[m];
// List<int[]>[] cols = new ArrayList[n];
// Set<int[]> rowSet = new HashSet<>();
// Set<int[]> colSet = new HashSet<>();
// Set<int[]> boardSet = new HashSet<>();
//
// for (int i = 0; i < m; ++i)
// rows[i] = new ArrayList<>();
//
// for (int j = 0; j < n; ++j)
// cols[j] = new ArrayList<>();
//
// for (int i = 0; i < m; ++i)
// for (int j = 0; j < n; ++j) {
// int[] cell = new int[] {board[i][j], i, j};
// rows[i].add(cell);
// cols[j].add(cell);
// }
//
// Comparator<int[]> comparator = Comparator.comparingInt(a -> - a[0]);
//
// for (List<int[]> row : rows) {
// row.sort(comparator);
// rowSet.addAll(row.subList(0, Math.min(3, row.size())));
// }
//
// for (List<int[]> col : cols) {
// col.sort(comparator);
// colSet.addAll(col.subList(0, Math.min(3, col.size())));
// }
//
// boardSet.addAll(rowSet);
// boardSet.retainAll(colSet);
//
// // At least 9 positions are required on the board to place 3 rooks such that
// // none can attack another.
// List<int[]> topNine = new ArrayList<>(boardSet);
// topNine.sort(comparator);
// topNine = topNine.subList(0, Math.min(9, topNine.size()));
//
// for (int i = 0; i < topNine.size(); ++i)
// for (int j = i + 1; j < topNine.size(); ++j)
// for (int k = j + 1; k < topNine.size(); ++k) {
// int[] t1 = topNine.get(i);
// int[] t2 = topNine.get(j);
// int[] t3 = topNine.get(k);
// if (t1[1] == t2[1] || t1[1] == t3[1] || t2[1] == t3[1] || //
// t1[2] == t2[2] || t1[2] == t3[2] || t2[2] == t3[2])
// continue;
// ans = Math.max(ans, (long) t1[0] + t2[0] + t3[0]);
// }
//
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.