Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 977 Bytes

maximum-product-subarray.md

File metadata and controls

32 lines (24 loc) · 977 Bytes
"""
  Problem Name : Maximum Product Subarray
  Problem URL : https://leetcode.com/problems/maximum-product-subarray/
  Description :
    Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.
    The test cases are generated so that the answer will fit in a 32-bit integer.

    A subarray is a contiguous subsequence of the array.
  Difficulty : Medium
  Language : Python3
  Category : Algorithms - Dynamic Programming
"""
class Solution:
    def maxProduct(self, nums: List[int]) -> int:
        
        if len(nums) < 2:
            return nums[0]
        
        maximum = maxi = mini = nums[0]
        
        for i in range(1, len(nums)):
            combinations = (nums[i], nums[i] * maxi, nums[i] * mini)
            maxi = max(combinations)
            mini = min(combinations)
            
            maximum = max(maximum, maxi)
            
        return maximum