LeetCode 题解工作台
乘积为正数的最长子数组长度
给你一个整数数组 nums ,请你求出乘积为正数的最长子数组的长度。 一个数组的子数组是由原数组中零个或者更多个连续数字组成的数组。 请你返回乘积为正数的最长子数组长度。 示例 1: 输入: nums = [1,-2,-3,4] 输出: 4 解释: 数组本身乘积就是正数,值为 24 。 示例 2: …
3
题型
5
代码语言
3
相关题
当前训练重点
中等 · 状态·转移·动态规划
答案摘要
我们定义两个长度为 的数组 和 ,其中 表示以 结尾的乘积为正数的最长子数组的长度,而 表示以 结尾的乘积为负数的最长子数组的长度。 初始时,如果 $\textit{nums}[0] > 0$,则 $f[0] = 1$,否则 $f[0] = 0$;如果 $\textit{nums}[0] < 0$,则 $g[0] = 1$,否则 $g[0] = 0$。我们初始化答案 $ans = f[…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你一个整数数组 nums ,请你求出乘积为正数的最长子数组的长度。
一个数组的子数组是由原数组中零个或者更多个连续数字组成的数组。
请你返回乘积为正数的最长子数组长度。
示例 1:
输入:nums = [1,-2,-3,4] 输出:4 解释:数组本身乘积就是正数,值为 24 。
示例 2:
输入:nums = [0,1,-2,-3,-4] 输出:3 解释:最长乘积为正数的子数组为 [1,-2,-3] ,乘积为 6 。 注意,我们不能把 0 也包括到子数组中,因为这样乘积为 0 ,不是正数。
示例 3:
输入:nums = [-1,-2,-3,0,1] 输出:2 解释:乘积为正数的最长子数组是 [-1,-2] 或者 [-2,-3] 。
提示:
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
解题思路
方法一:动态规划
我们定义两个长度为 的数组 和 ,其中 表示以 结尾的乘积为正数的最长子数组的长度,而 表示以 结尾的乘积为负数的最长子数组的长度。
初始时,如果 ,则 ,否则 ;如果 ,则 ,否则 。我们初始化答案 。
接下来,我们从 开始遍历数组 ,对于每个 ,我们有以下几种情况:
- 如果 ,那么 可以由 转移而来,即 ,而 的值取决于 是否为 ,如果 ,则 ,否则 ;
- 如果 ,那么 的值取决于 是否为 ,如果 ,则 ,否则 ,而 可以由 转移而来,即 。
- 然后,我们更新答案 。
遍历结束后,返回答案 即可。
时间复杂度 ,空间复杂度 。其中 为数组 的长度。
class Solution:
def getMaxLen(self, nums: List[int]) -> int:
n = len(nums)
f = [0] * n
g = [0] * n
f[0] = int(nums[0] > 0)
g[0] = int(nums[0] < 0)
ans = f[0]
for i in range(1, n):
if nums[i] > 0:
f[i] = f[i - 1] + 1
g[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1
elif nums[i] < 0:
f[i] = 0 if g[i - 1] == 0 else g[i - 1] + 1
g[i] = f[i - 1] + 1
ans = max(ans, f[i])
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Look for a solution that can split the array by zeroes and uses dynamic programming to keep track of the lengths of positive subarrays.
- question_mark
Check if the candidate handles negative numbers and updates the product state accordingly.
- question_mark
The candidate should be able to distinguish between the greedy approach for splitting the array and dynamic programming for tracking subarray lengths.
常见陷阱
外企场景- error
Failing to handle zeroes correctly, treating them as part of subarrays.
- error
Incorrectly updating the state for negative numbers, leading to wrong subarray length calculations.
- error
Not splitting the array at zeroes, which results in incorrect handling of subarrays that contain zeroes.
进阶变体
外企场景- arrow_right_alt
Implementing the solution without using dynamic programming, focusing only on greedy subarray splitting.
- arrow_right_alt
Modifying the approach to return the subarray itself, not just the length.
- arrow_right_alt
Optimizing the space complexity to O(1) by avoiding extra storage for the state.