LeetCode 题解工作台

合并后数组中的最大元素

给你一个下标从 0 开始、由正整数组成的数组 nums 。 你可以在数组上执行下述操作 任意 次: 选中一个同时满足 0 和 nums[i] 的下标 i 。将元素 nums[i + 1] 替换为 nums[i] + nums[i + 1] ,并从数组中删除元素 nums[i] 。 返回你可以从最终数…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

根据题目描述,为了最大化合并后的数组中的最大元素,我们应该先合并右侧的元素,使得右侧的元素尽可能大,从而尽可能多地执行合并操作,最终得到最大的元素。 因此,我们可以从右向左遍历数组,对于每个位置 ,其中 $i \in [0, n - 2]$,如果 $nums[i] \leq nums[i + 1]$,我们就将 更新为 $nums[i] + nums[i + 1]$。这样做,相当于将 与 $nu…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始、由正整数组成的数组 nums

你可以在数组上执行下述操作 任意 次:

  • 选中一个同时满足 0 <= i < nums.length - 1nums[i] <= nums[i + 1] 的下标 i 。将元素 nums[i + 1] 替换为 nums[i] + nums[i + 1] ,并从数组中删除元素 nums[i]

返回你可以从最终数组中获得的 最大 元素的值。

 

示例 1:

输入:nums = [2,3,7,9,3]
输出:21
解释:我们可以在数组上执行下述操作:
- 选中 i = 0 ,得到数组 nums = [5,7,9,3] 。
- 选中 i = 1 ,得到数组 nums = [5,16,3] 。
- 选中 i = 0 ,得到数组 nums = [21,3] 。
最终数组中的最大元素是 21 。可以证明我们无法获得更大的元素。

示例 2:

输入:nums = [5,3,3]
输出:11
解释:我们可以在数组上执行下述操作:
- 选中 i = 1 ,得到数组 nums = [5,6] 。
- 选中 i = 0 ,得到数组 nums = [11] 。
最终数组中只有一个元素,即 11 。

 

提示:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 106
lightbulb

解题思路

方法一:倒序合并

根据题目描述,为了最大化合并后的数组中的最大元素,我们应该先合并右侧的元素,使得右侧的元素尽可能大,从而尽可能多地执行合并操作,最终得到最大的元素。

因此,我们可以从右向左遍历数组,对于每个位置 ii,其中 i[0,n2]i \in [0, n - 2],如果 nums[i]nums[i+1]nums[i] \leq nums[i + 1],我们就将 nums[i]nums[i] 更新为 nums[i]+nums[i+1]nums[i] + nums[i + 1]。这样做,相当于将 nums[i]nums[i]nums[i+1]nums[i + 1] 合并,并且删掉 nums[i]nums[i]

最终,数组中的最大元素就是合并后的数组中的最大元素。

时间复杂度 O(n)O(n),其中 nn 为数组的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
class Solution:
    def maxArrayValue(self, nums: List[int]) -> int:
        for i in range(len(nums) - 2, -1, -1):
            if nums[i] <= nums[i + 1]:
                nums[i] += nums[i + 1]
        return max(nums)
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Look for the candidate's ability to identify and implement greedy choices effectively.

  • question_mark

    Candidates who start from the end of the array and merge adjacent elements will demonstrate a good understanding of the problem.

  • question_mark

    Watch for the candidate's explanation of validating each merge operation to avoid suboptimal choices.

warning

常见陷阱

外企场景
  • error

    Failing to correctly merge adjacent elements starting from the end of the array leads to suboptimal results.

  • error

    Not keeping track of the largest possible value during each merge operation can result in missed opportunities to maximize the final element.

  • error

    Overcomplicating the merge strategy or trying to apply complex algorithms that deviate from the greedy approach can slow down the solution.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Varying the number of merges allowed (limiting merges or setting maximum operations)

  • arrow_right_alt

    Using different array sizes and testing edge cases like minimal and maximal input sizes

  • arrow_right_alt

    Exploring the problem with non-integer elements or arrays with a mix of large and small values

help

常见问题

外企场景

合并后数组中的最大元素题解:贪心·invariant | LeetCode #2789 中等