152. 乘积最大子数组 - 力扣(LeetCode)

大佬图示:

代码

python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 21:37 
# "Stay hungry,stay foolish."

class Solution(object):
    def maxProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res = max(nums)
        curMin, curMax = 1, 1

        for n in nums:
            if n == 0:
                curMin, curMax = 1, 1
                continue
            temp = n * curMax # 防止提前修改的curMax污染curMin
            curMax = max(n * curMax, n * curMin, n)
            curMin = min(temp, n * curMin, n)
            res = max(res, curMax)
        return res
num = Solution().maxProduct([-4,-3,-2])
print(num)

结果

解题步骤:

相关推荐
NEFU_Jyj3 小时前
牛客周赛R168
算法
文丑颜不良啊11 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z11 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH12 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶13 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊13 小时前
LeetCode2161~2180题目解析
java·leetcode
雪落漂泊13 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
文丑颜不良啊15 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发15 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview