maximum_product_subarray

  • 2022-12-14
  • 浏览 (531)

maximum_product_subarray.py 源码

# 乘积最大子数组
from typing import List


class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        res, maxim, minim = nums[0], nums[0], nums[0]
        for i in range(1, len(nums)):
            if nums[i] < 0:
                maxim, minim = minim, maxim
            maxim = max(nums[i], maxim * nums[i])
            minim = min(nums[i], minim * nums[i])
            res = max(maxim, res)
        return res

你可能感兴趣的文章

coin_change

decode_ways

edit_distance

0  赞