Maximum Value Sum by Placing Three Rooks I - Problem
Maximum Value Sum by Placing Three Rooks I

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
Maximum Value Sum - Three Non-Attacking Rooks4ร—5 Example Board1058123715691142131141683187โ™–โ™–โ™–Optimal Solution AnalysisSelected Positions:Rook 1: Row 0, Col 3 โ†’ Value: 12Rook 2: Row 1, Col 1 โ†’ Value: 15Rook 3: Row 2, Col 4 โ†’ Value: 14Total Sum: 12 + 15 + 14 = 41โœ“ All rooks in different rows and columnsAlgorithm Steps1. Row Selection: Choose 3 from 4 rowsC(4,3) = 4 combinations: {0,1,2}, {0,1,3}, {0,2,3}, {1,2,3}2. Column Selection: Choose 3 from 5 columnsC(5,3) = 10 combinations: {0,1,2}, {0,1,3}, {0,1,4}, ...3. Assignment: Match rows to columnsFor each row-col combination, try all 6 permutations4. Optimization: Track maximum sumTotal combinations: 4 ร— 10 ร— 6 = 240 checks๐ŸŽฏ Key Insight: Systematic enumeration is efficient for small fixed k (k=3 rooks)
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.
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
47.3K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen