Maximum Value Sum by Placing Three Rooks I - Problem
Maximum Value Sum by Placing Three Rooks I
You're given an
In chess, rooks can attack any piece in the same row or column. Your task is to place exactly three rooks on the chessboard such that:
โข No two rooks attack each other (they must be in different rows AND different columns)
โข The sum of values on which the rooks are placed is maximized
Goal: Return the maximum possible sum of cell values where three non-attacking rooks can be placed.
Example: If you have a 3x3 board with values [[1,2,3],[4,5,6],[7,8,9]], you could place rooks at positions (0,2), (1,0), and (2,1) to get values 3+4+8=15.
You're given an
m x n 2D array board representing a chessboard, where board[i][j] represents the value of the cell at position (i, j). In chess, rooks can attack any piece in the same row or column. Your task is to place exactly three rooks on the chessboard such that:
โข No two rooks attack each other (they must be in different rows AND different columns)
โข The sum of values on which the rooks are placed is maximized
Goal: Return the maximum possible sum of cell values where three non-attacking rooks can be placed.
Example: If you have a 3x3 board with values [[1,2,3],[4,5,6],[7,8,9]], you could place rooks at positions (0,2), (1,0), and (2,1) to get values 3+4+8=15.
Input & Output
example_1.py โ Basic 3x3 Grid
$
Input:
board = [[1,2,3],[4,5,6],[7,8,9]]
โบ
Output:
15
๐ก Note:
The optimal placement is rooks at positions (0,2), (1,0), (2,1) giving us values 3 + 4 + 8 = 15. No two rooks are in the same row or column.
example_2.py โ Negative Values
$
Input:
board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
โบ
Output:
4
๐ก Note:
Place rooks at (0,1), (1,3), (2,1) to get values 1 + 1 + 2 = 4. This avoids the negative values while satisfying the non-attacking constraint.
example_3.py โ Large Values
$
Input:
board = [[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]
โบ
Output:
3
๐ก Note:
All values are the same, so any valid placement of 3 non-attacking rooks gives the same sum of 3. For example: (0,0), (1,1), (2,2).
Constraints
- 3 โค m, n โค 100
- -106 โค board[i][j] โค 106
- The board has at least 3 rows and 3 columns
- Exactly 3 rooks must be placed
Visualization
Tap to expand
Understanding the Visualization
1
Identify Constraint
Rooks attack along rows and columns, so each rook must be in a different row and different column
2
Enumerate Combinations
Systematically try all ways to choose 3 rows and 3 columns from the available options
3
Optimize Assignment
For each row-column combination, find the best way to assign rows to columns to maximize sum
4
Track Maximum
Keep track of the highest sum found across all valid placements
Key Takeaway
๐ฏ Key Insight: Since we only need to place exactly 3 rooks, we can efficiently enumerate all combinations of 3 rows and 3 columns, then find the optimal assignment between them. This structured approach scales much better than naive brute force.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code