LeetCode 题解工作台

乘积最大子数组

给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续 子数组 (该子数组中至少包含一个数字),并返回该子数组所对应的乘积。 测试用例的答案是一个 32-位 整数。 请注意 ,一个只包含一个元素的数组的乘积是这个元素的值。 示例 1: 输入: nums = [2,3,-2,4] 输出: 6 …

category

2

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

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

bolt

答案摘要

我们定义两个变量 和 ,其中 表示以 结尾的乘积最大子数组的乘积,而 表示以 结尾的乘积最小子数组的乘积。初始时 和 都等于 。答案为所有 中的最大值。 从 开始,我们可以考虑将第 个数 添加到前面的乘积最大或者乘积最小的子数组乘积的后面,或者单独作为一个子数组乘积(即此时子数组长度只有 )。我们将此前的乘积最大值记为 ,乘积最小值记为 ,那么 $f = \max(f, ff…

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续 子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。

测试用例的答案是一个 32-位 整数。

请注意,一个只包含一个元素的数组的乘积是这个元素的值。

 

示例 1:

输入: nums = [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6。

示例 2:

输入: nums = [-2,0,-1]
输出: 0
解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。

 

提示:

  • 1 <= nums.length <= 2 * 104
  • -10 <= nums[i] <= 10
  • nums 的任何子数组的乘积都 保证 是一个 32-位 整数
lightbulb

解题思路

方法一:动态规划

我们定义两个变量 ffgg,其中 ff 表示以 nums[i]nums[i] 结尾的乘积最大子数组的乘积,而 gg 表示以 nums[i]nums[i] 结尾的乘积最小子数组的乘积。初始时 ffgg 都等于 nums[0]nums[0]。答案为所有 ff 中的最大值。

i=1i=1 开始,我们可以考虑将第 ii 个数 nums[i]nums[i] 添加到前面的乘积最大或者乘积最小的子数组乘积的后面,或者单独作为一个子数组乘积(即此时子数组长度只有 11)。我们将此前的乘积最大值记为 ffff,乘积最小值记为 gggg,那么 f=max(f,ff×nums[i],gg×nums[i])f = \max(f, ff \times nums[i], gg \times nums[i]),而 g=min(g,ff×nums[i],gg×nums[i])g = \min(g, ff \times nums[i], gg \times nums[i])。接下来,我们更新答案 ans=max(ans,f)ans = \max(ans, f),然后继续计算下一个位置。

最后的答案即为 ansans

时间复杂度 O(n)O(n),其中 nn 是数组 numsnums 的长度。我们只需要遍历数组一次即可求得答案。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        ans = f = g = nums[0]
        for x in nums[1:]:
            ff, gg = f, g
            f = max(x, ff * x, gg * x)
            g = min(x, ff * x, gg * x)
            ans = max(ans, f)
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Can the candidate explain how negative numbers affect the product and how this is handled in the solution?

  • question_mark

    Does the candidate demonstrate an understanding of dynamic programming techniques, specifically state transition?

  • question_mark

    How efficiently can the candidate discuss and apply space optimization in dynamic programming problems?

warning

常见陷阱

外企场景
  • error

    Forgetting to handle negative numbers correctly and assuming that the largest product will always be from the largest values.

  • error

    Not considering zero as a product, which could reset the subarray product.

  • error

    Using excessive space by storing all subarray products instead of just tracking the current max and min products.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the problem to consider circular subarrays where the subarray can wrap around the array.

  • arrow_right_alt

    Extend the problem to handle larger integer ranges, potentially using BigInteger or similar data types.

  • arrow_right_alt

    Introduce a constraint where the array has multiple zeroes, and check if the algorithm handles this efficiently.

help

常见问题

外企场景

乘积最大子数组题解:状态·转移·动态规划 | LeetCode #152 中等