LeetCode 题解工作台

相同分数的最大操作数目 II

给你一个整数数组 nums ,如果 nums 至少 包含 2 个元素,你可以执行以下操作中的 任意 一个: 选择 nums 中最前面两个元素并且删除它们。 选择 nums 中最后两个元素并且删除它们。 选择 nums 中第一个和最后一个元素并且删除它们。 一次操作的 分数 是被删除元素的和。 在确保…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

分数 的取值有三种情况,分别是 $s = nums[0] + nums[1]$, $s = nums[0] + nums[n-1]$, $s = nums[n-1] + nums[n-2]$。我们可以针对这三种情况,分别进行记忆化搜索。 我们设计一个函数 $dfs(i, j)$,表示在分数为 的情况下,从下标 到下标 的最大操作次数。函数 $dfs(i, j)$ 的执行过程如下:

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 nums ,如果 nums 至少 包含 2 个元素,你可以执行以下操作中的 任意 一个:

  • 选择 nums 中最前面两个元素并且删除它们。
  • 选择 nums 中最后两个元素并且删除它们。
  • 选择 nums 中第一个和最后一个元素并且删除它们。

一次操作的 分数 是被删除元素的和。

在确保 所有操作分数相同 的前提下,请你求出 最多 能进行多少次操作。

请你返回按照上述要求 最多 可以进行的操作次数。

 

示例 1:

输入:nums = [3,2,1,2,3,4]
输出:3
解释:我们执行以下操作:
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [1,2,3,4] 。
- 删除第一个元素和最后一个元素,分数为 1 + 4 = 5 ,nums = [2,3] 。
- 删除第一个元素和最后一个元素,分数为 2 + 3 = 5 ,nums = [] 。
由于 nums 为空,我们无法继续进行任何操作。

示例 2:

输入:nums = [3,2,6,1,4]
输出:2
解释:我们执行以下操作:
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [6,1,4] 。
- 删除最后两个元素,分数为 1 + 4 = 5 ,nums = [6] 。
至多进行 2 次操作。

 

提示:

  • 2 <= nums.length <= 2000
  • 1 <= nums[i] <= 1000
lightbulb

解题思路

方法一:记忆化搜索

分数 ss 的取值有三种情况,分别是 s=nums[0]+nums[1]s = nums[0] + nums[1], s=nums[0]+nums[n1]s = nums[0] + nums[n-1], s=nums[n1]+nums[n2]s = nums[n-1] + nums[n-2]。我们可以针对这三种情况,分别进行记忆化搜索。

我们设计一个函数 dfs(i,j)dfs(i, j),表示在分数为 ss 的情况下,从下标 ii 到下标 jj 的最大操作次数。函数 dfs(i,j)dfs(i, j) 的执行过程如下:

  • 如果 ji<1j - i < 1,表示区间 [i,j][i, j] 的长度小于 22,无法进行任何操作,返回 00
  • 如果 nums[i]+nums[i+1]=snums[i] + nums[i+1] = s,表示可以删除下标 ii 和下标 i+1i+1 的元素,此时最大操作次数为 1+dfs(i+2,j)1 + dfs(i+2, j)
  • 如果 nums[i]+nums[j]=snums[i] + nums[j] = s,表示可以删除下标 ii 和下标 jj 的元素,此时最大操作次数为 1+dfs(i+1,j1)1 + dfs(i+1, j-1)
  • 如果 nums[j1]+nums[j]=snums[j-1] + nums[j] = s,表示可以删除下标 j1j-1 和下标 jj 的元素,此时最大操作次数为 1+dfs(i,j2)1 + dfs(i, j-2)
  • 返回以上的最大值即可。

最后,我们分别计算三种情况的最大操作次数,取最大值返回即可。

时间复杂度 O(n2)O(n^2),空间复杂度 O(n2)O(n^2)。其中 nn 是数组 numsnums 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
    def maxOperations(self, nums: List[int]) -> int:
        @cache
        def dfs(i: int, j: int, s: int) -> int:
            if j - i < 1:
                return 0
            ans = 0
            if nums[i] + nums[i + 1] == s:
                ans = max(ans, 1 + dfs(i + 2, j, s))
            if nums[i] + nums[j] == s:
                ans = max(ans, 1 + dfs(i + 1, j - 1, s))
            if nums[j - 1] + nums[j] == s:
                ans = max(ans, 1 + dfs(i, j - 2, s))
            return ans

        n = len(nums)
        a = dfs(2, n - 1, nums[0] + nums[1])
        b = dfs(0, n - 3, nums[-1] + nums[-2])
        c = dfs(1, n - 2, nums[0] + nums[-1])
        return 1 + max(a, b, c)
speed

复杂度分析

指标
时间and space complexity depends on the DP implementation. A naive recursive approach may reach O(2^n), but memoization reduces repeated state computations. With optimized DP, expect time O(n^3) in worst case and space O(n^2) for storing subarray results.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Focus on state transition dynamic programming with memoization to avoid redundant calculations.

  • question_mark

    Consider how pair selection order affects maximum operations with the same score.

  • question_mark

    Highlight that after choosing the first operation, the score of subsequent operations is fixed and drives DP decisions.

warning

常见陷阱

外企场景
  • error

    Forgetting to fix the score after the first operation, leading to inconsistent totals.

  • error

    Not using memoization, which causes timeouts on larger arrays.

  • error

    Assuming any pair sum works without verifying it maintains the same score across all operations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow operations that remove more than two elements while keeping the score consistent.

  • arrow_right_alt

    Count the maximum operations but with a limited number of total deletions.

  • arrow_right_alt

    Extend to arrays with negative integers and track valid operation sequences.

help

常见问题

外企场景

相同分数的最大操作数目 II题解:状态·转移·动态规划 | LeetCode #3040 中等