LeetCode 题解工作台
放三个车的价值之和最大 II
给你一个 m x n 的二维整数数组 board ,它表示一个国际象棋棋盘,其中 board[i][j] 表示格子 (i, j) 的 价值 。 处于 同一行 或者 同一列 车会互相 攻击 。你需要在棋盘上放三个车,确保它们两两之间都 无法互相攻击 。 请你返回满足上述条件下,三个车所在格子 值 之和…
4
题型
0
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你一个 m x n 的二维整数数组 board ,它表示一个国际象棋棋盘,其中 board[i][j] 表示格子 (i, j) 的 价值 。
处于 同一行 或者 同一列 车会互相 攻击 。你需要在棋盘上放三个车,确保它们两两之间都 无法互相攻击 。
请你返回满足上述条件下,三个车所在格子 值 之和 最大 为多少。
示例 1:
输入:board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
输出:4
解释:

我们可以将车分别放在格子 (0, 2) ,(1, 3) 和 (2, 1) 处,价值之和为 1 + 1 + 2 = 4 。
示例 2:
输入:board = [[1,2,3],[4,5,6],[7,8,9]]
输出:15
解释:
我们可以将车分别放在格子 (0, 0) ,(1, 1) 和 (2, 2) 处,价值之和为 1 + 5 + 9 = 15 。
示例 3:
输入:board = [[1,1,1],[1,1,1],[1,1,1]]
输出:3
解释:
我们可以将车分别放在格子 (0, 2) ,(1, 1) 和 (2, 0) 处,价值之和为 1 + 1 + 1 = 3 。
提示:
3 <= m == board.length <= 5003 <= n == board[i].length <= 500-109 <= board[i][j] <= 109
解题思路
方法一
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Check if the candidate can identify the dynamic programming nature of this problem.
- question_mark
Evaluate the candidate's understanding of row-wise maximization and state transitions.
- question_mark
Observe the candidate's ability to optimize space complexity while maintaining efficiency.
常见陷阱
外企场景- error
Failing to track the top three values in each row and revisiting the same values repeatedly.
- error
Not handling row and column uniqueness correctly, leading to invalid rook placements.
- error
Overcomplicating the solution by attempting brute force instead of utilizing dynamic programming optimizations.
进阶变体
外企场景- arrow_right_alt
Increasing the number of rooks to be placed, which adds complexity to the dynamic programming approach.
- arrow_right_alt
Considering different board sizes, such as a larger m x n array, to challenge the space and time optimizations.
- arrow_right_alt
Allowing for variable rook placements, where more complex rules determine valid positions, still using dynamic programming.