Maximum Value Sum by Placing Three Rooks II - Problem
Chess Optimization Challenge: You're given an m × n chessboard where each cell board[i][j] contains a numerical value. Your task is to place exactly three rooks on the board such that they don't attack each other (no two rooks can be in the same row or column).

The goal is to maximize the sum of the values in the cells where you place the rooks. This is a classic combinatorial optimization problem that tests your understanding of constraints and efficient enumeration.

Key Rules:
• Place exactly 3 rooks
• No two rooks in the same row
• No two rooks in the same column
• Maximize the sum of cell values

Input & Output

example_1.py — Python
$ Input: [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
Output: 4
💡 Note: Place rooks at (0,2), (1,1), (2,3) with values 1+1+1=3, or at (0,3), (1,1), (2,1) with values 1+1+2=4. The maximum is 4.
example_2.py — Python
$ Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: 15
💡 Note: Place rooks at (0,2), (1,1), (2,0) with values 3+5+7=15. This gives the maximum possible sum.
example_3.py — Python
$ Input: [[1,1],[1,1]]
Output: 2
💡 Note: Only 2 rows available, so we can place at most 2 rooks. With exactly 3 rooks required, we need at least 3 rows. This case would be invalid for the problem constraints.

Constraints

  • 3 ≤ m, n ≤ 100 (board dimensions)
  • -105 ≤ board[i][j] ≤ 105 (cell values)
  • You must place exactly 3 rooks
  • Each rook must be in a different row and different column

Visualization

Tap to expand
Three Rooks StrategyBoard Values:-3111-31-31-3211Row 0Row 1Row 2Optimal Placement:Rook 1: (0,3) = 1Rook 2: (1,1) = 1Rook 3: (2,1) = 2Total Sum: 4
Understanding the Visualization
1
Analyze Board
Identify high-value positions and constraints
2
Fix First Position
Choose a strategic first rook placement
3
Optimize Remaining
Find optimal placement for remaining 2 rooks
4
Combine Results
Sum values and track maximum across all strategies
Key Takeaway
🎯 Key Insight: By systematically enumerating positions and using smart precomputation, we can solve this constraint optimization problem efficiently while guaranteeing the optimal solution.
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
28.4K Views
Medium Frequency
~25 min Avg. Time
856 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