LeetCode 题解工作台

放三个车的价值之和最大 I

给你一个 m x n 的二维整数数组 board ,它表示一个国际象棋棋盘,其中 board[i][j] 表示格子 (i, j) 的 价值 。 处于 同一行 或者 同一列 车会互相 攻击 。你需要在棋盘上放三个车,确保它们两两之间都 无法互相攻击 。 请你返回满足上述条件下,三个车所在格子 值 之和…

category

4

题型

code_blocks

0

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个 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 <= 100
  • 3 <= n == board[i].length <= 100
  • -109 <= board[i][j] <= 109
lightbulb

解题思路

方法一

1
2

speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Assess the candidate's understanding of dynamic programming and state transitions in grid-based problems.

  • question_mark

    Evaluate the ability to manage constraints like row and column restrictions when placing objects on a grid.

  • question_mark

    Look for clear reasoning behind optimizing the search for the best rook placements using pre-calculated values.

warning

常见陷阱

外企场景
  • error

    Failing to properly handle row and column restrictions, leading to incorrect placements.

  • error

    Overlooking edge cases, such as negative values in the board or having too few rows and columns.

  • error

    Not optimizing by storing the largest values, leading to unnecessary recalculations and inefficiencies.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Instead of three rooks, consider placing a different number of rooks, adjusting the dynamic programming approach accordingly.

  • arrow_right_alt

    Extend the problem to larger boards with more rows and columns, requiring more sophisticated optimizations.

  • arrow_right_alt

    Introduce varying rook values or constraints to test the algorithm's adaptability to different scenarios.

help

常见问题

外企场景

放三个车的价值之和最大 I题解:状态·转移·动态规划 | LeetCode #3256 困难