题目链接 | 152. 乘积最大子数组 |
---|---|
思路 | 最大子数组和-简单变体 |
题解链接 | 动态规划 |
关键点 | 无 |
时间复杂度 | \(O(n)\) |
空间复杂度 | \(O(1)\) |
代码实现:
class Solution:
def maxProduct(self, nums: List[int]) -> int:
answer = premax = premin = nums[0]
for num in nums[1:]:
cur_max = max(premax * num, premin * num, num)
cur_min = min(premax * num, premin * num, num)
answer = max(answer, cur_max)
premax, premin = cur_max, cur_min
return answer
标签:152,乘积,premin,max,num,数组,answer,premax,cur
From: https://www.cnblogs.com/WrRan/p/18419557